MVC3 中的 URL 重写 [英] URL rewriting in MVC3

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

问题描述

我正在使用 MVC3 为当地大学开展一个项目.我遇到了一个需求,我陷入困境并且找不到任何出路.
假设我的 URL 是 www.abc.com
要求是,如果我们在 URL 后输入教师姓名,我们将获得教师的详细视图,例如:
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.

只需为它创建一个路由:

Just create a route for it:

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

请注意,这将与任何其他 /Whatever 网址(例如,/About)发生冲突;为避免这种情况,您可以使用 my 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.

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

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