MVC3 和重写 [英] MVC3 and Rewrites
问题描述
我正在编写一个 MVC3 应用程序,它需要以 http://[server]/[City]-[State]/[some term]/的形式使用 URL 重写.
I'm writing an MVC3 application that will need to make use of URL rewriting in the form of http://[server]/[City]-[State]/[some term]/ .
据我所知,MVC3 包含一个使用 {controler}/{action}/{id} 的路由引擎,该引擎在 Global.asax 文件中定义:
As I understand it, MVC3 contains a routing engine that uses {controler}/{action}/{id} which is defined in the Global.asax file:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
传统上(在非 MVC 应用程序中),我会使用一些 URL 重写风格来解码一个 url,例如 http://www.myserver.com/City-State/somesearch/ 到查询字符串参数,看起来像这样:http://www.myserver.com/city=City&state=State&query=somesearch
Traditionally (in a non-MVC app), I would use some URL rewriting flavor to decode a url such as http://www.myserver.com/City-State/somesearch/ to querystring parameters that look something like this: http://www.myserver.com/city=City&state=State&query=somesearch
请记住,此请求将来自 http://www.myserver.com/Home
Keep in mind that this request would be coming from http://www.myserver.com/Home
这可以在无需指定控制器的情况下完成吗...像这样:
Can this can be accomplished without having to specify a controller... something like this:
routes.MapRoute(
"Results",
"{city}-{state}/{searchTerm}",
new { controller = "Results", action = "Search" }
);
...还是最好列出控制器?
... or is it really best to have the controller listed?
您如何在 MVC3 环境中处理此问题?
How do you handle this in an MVC3 environment?
谢谢.
推荐答案
asp.net MVC3 中的 URL 重写:-您可以在 Global.asax 文件中编写用于 url 重写的代码:-
URL rewriting in asp.net MVC3:- you can write code for url rewriting in Global.asax file :-
//Default url
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"",
new { controller = "Home", action = "Index", id = "" }
);
//others url rewriting you want
RouteTable.Routes.MapRoute(null, "Search/{City_State}/{ID}", new { controller = "Home", action = "Search" });
这篇关于MVC3 和重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!