简体   繁体   English

用扩展重写asp.net-mvc4(剃刀)中的URL

[英]Rewriting url in asp.net-mvc4 (razor) with extention

Default of asp.net-mvc4 is http://domainname.com/products/1 with routes 带有路由的asp.net-mvc4的默认值为http://domainname.com/products/1

routes.MapRoute(
    name: "Products",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);

and I want to rewrite to http://domainname.com/products/1.html that has .html extention . 并且我想重写为具有.html扩展名的http://domainname.com/products/1.html Any ideas for this? 有什么想法吗?

Do you mean this: 你的意思是:

routes.MapRoute(
    name: "Products",
    url: "{controller}/{action}/{id}.html",
    defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);

Why would you want to put .html extension to a query string or route parameter? 为什么要在查询字符串或路由参数中添加.html扩展名?

if you take a website with html files, the extension is with the page not with the parameters. 如果您使用的是带有html文件的网站,则扩展名包含页面,而不包含参数。 Don't know if you have any specific requirement to put it always at the end of the url. 不知道您是否有任何特定要求始终将其放在网址末尾。 but it doesn't make any sense. 但这没有任何意义。

routes.MapRoute(
name: "Products",
url: "{controller}/{action}.html/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);

you might have problems with iis after doing the above, please take a look at this discussion as well. 完成上述操作后,您可能会遇到iis的问题,请也参阅此讨论。 ASP.NET MVC Routing - add .html extension to routes ASP.NET MVC路由-将.html扩展名添加到路由

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM