URL重写在MVC3 [英] URL rewriting in MVC3

查看:118
本文介绍了URL重写在MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC3当地一所大学的项目。我曾经穿过在此我坚持,找不到任何出路的要求来了。结果
让我们假设我的网址是www.abc.com结果
要求是,如果我们在U​​RL后键入老师的名字,我们得到老师的详细视图,如:结果
www.abc.com/john结果
www.abc.com/smith结果
我问像www.abc.com/teacher=john选项,但它已被拒绝。结果
这是相关的URL重写或其他一些出路的东西,因为可以有很多教师数据库,所以我不能让在控制器的每一位老师的方法。
任何人都可以请指导我对于这种情况?
结果
亲切的问候

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

推荐答案

MVC做到这一点本身。

MVC does this natively.

只要创建一个路由吧:

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

请注意,这将与任何其他冲突/无论的URL(例如, /关于);要避免这种情况,您可以使用我的 MapDefaultController( )扩展此人之前映射为一个特定的控制器的路由。

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.

这篇关于URL重写在MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆