这个Global.asax路由设置有什么问题?

时间:2012-04-27 08:43:02

标签: asp.net url-routing asp.net-routing

我可能期望从ASP.NET中获得太多,但在Apache中,重写URL所以请求类似的东西是微不足道的: http://mysite/myfolder/mypage/niceurlparameter 实际上设法提供静态页面http://mysite/mypage.html

我如何在Global.asax中执行此操作?

我试过这个:

RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}/*", "~/myfolder/{page}.html");

但是当我请求http://mysite/myfolder/mypage/niceurlparameter时,它会一直返回404。

然而,这有效:

RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}.html/*", "~/myfolder/{page}.html");

所以我在申请http://mysite/myfolder/mypage.html/niceurlparameter时会收到mypage.html。

我只想摆脱网址中的“.html”部分。我错过了什么?

更新: 出于某种原因,在前一种情况下,'*'通配符尚未被接受。

更改为:

RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}/{whatever}", "~/myfolder/{page}.html");

似乎将请求路由到html页面,但后来我收到错误:

There is no build provider registered for the extension '.html'. 

为什么在世界上它只适用于前一种情况(在URL中使用html),而不是在html被遗漏的情况下?

1 个答案:

答案 0 :(得分:3)

  

没有为扩展名'.html'

注册的构建提供程序

您收到此错误,因为IIS应直接处理静态html文件。但是,在您的情况下,ASP.NET MVC框架正在尝试处理类型为.html的文件,但它无法处理。

因此,如果您认为这是您需要做的,则必须创建一个新的提供程序并在web.config文件中注册。看看这个

Custom file extensions for ASP.NET - help needed!

您只需将静态html内容更改为.aspx文件即可。一个简单的复制和粘贴将完成这项工作,它应该工作正常。它将知道如何处理文件类型。

相关问题