URL rewriting in MVC3

感情迁移 提交于 2019-12-04 02:01:33

问题


I am working on a project for a local college using MVC3. I have came across a requirement at which I am stuck and can't find any wayout.
Let suppose my URL is www.abc.com
The requirement is that if we type teacher name after the URL we get the detailed view of the teacher, like:
www.abc.com/john
www.abc.com/smith
I asked for option like www.abc.com/teacher=john but it has been rejected.
Is this something relevant to URL rewriting or some other wayout, as there can be many teachers in database so I can't make methods in controllers for every teacher. Can anyone please guide me for this scenario?
Kind Regards


回答1:


MVC does this natively.

Just create a route for it:

routes.MapRoute(
    "Teacher route",
    "/{teacher}",
    new { controller = "SomeController", action = "SomeAction" }
)

Note that this will conflict with any other /Whatever URLs (eg, /About); to avoid that, you can use my MapDefaultController() extension to map a route for a specific controller before this one.



来源:https://stackoverflow.com/questions/10637732/url-rewriting-in-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!