扩展 Asp.Net MVC 路由机制

发布于 2024-07-23 12:19:01 字数 1276 浏览 7 评论 0原文

我发现 ASP.Net mvc 的路由机制存在限制,我正在尝试找到解决方法。

我在此处发布了相关问题关于我遇到的问题。

问题的要点是以 . (句点)永远不会被默认路由机制处理。 总是抛出“找不到资源”错误。 例如,它无法处理这些网址:

http://www.wikipediamaze.com/wiki/Washington,_D.C.
http://www.wikipediamaze.com/wiki/anythingendinglikethis.

如果我将其更改为像这样的查询字符串参数,它可以正常工作:

http://www.wikipediamaze.com/wiki/?topic=Washington,_D.C.

我试图在路由机制中找到一个可扩展点,这将帮助我解决此问题。 我尝试过类似的其他解决方案:

//Global.asax.cs
protected void Application_Error()
{
     var url = HttpContext.Current.Request.RawUrl;
     if(TopicRegex.IsMatch(url))
     {
         var fixedUrl = FixUrlPath(url);

         //This throws an error
         Response.Redirect(fixedUrl);

         //This also throws an error
         Server.Transfer(fixedUrl );
      }
}

我猜测 Response.Redirect 和 Server.Transfer 会抛出错误,因为在 MVC 中您应该从控制器调用 RedirectToAction 方法。 当然,我甚至无法到达控制器。

考虑到维基百科使用的 Apache 服务器可以很好地处理这个问题,这似乎是一个相当大的限制。 尝试一下http://en.wikipedia.org/wiki/Washington,_D.C. 如果有人可以在这里提供一些帮助,我将不胜感激。

I've found a limitation in the routing mechanism for ASP.Net mvc and I'm trying to find a workaround.

I posted a related question here about the issue I was having.

The gist of the problem is that routes that end with a . (period) are never handled by the default routing mechanism. A "Resource Cannot Be Found" error is always thrown. For example it cannot handle these urls:

http://www.wikipediamaze.com/wiki/Washington,_D.C.
http://www.wikipediamaze.com/wiki/anythingendinglikethis.

if I change it to querystring parameter like this it works fine:

http://www.wikipediamaze.com/wiki/?topic=Washington,_D.C.

I'm trying to find an extensibility point in the routing mechanism that will help me resolve this issue. I've tried other solutions like this:

//Global.asax.cs
protected void Application_Error()
{
     var url = HttpContext.Current.Request.RawUrl;
     if(TopicRegex.IsMatch(url))
     {
         var fixedUrl = FixUrlPath(url);

         //This throws an error
         Response.Redirect(fixedUrl);

         //This also throws an error
         Server.Transfer(fixedUrl );
      }
}

I'm guessing that the Response.Redirect and Server.Transfer throw errors because in MVC you should be calling the RedirectToAction methods from the controller. Of course I can't even get to the controller.

This seems to be a pretty big limitation considering the Apache server that Wikipedia uses handles this just fine. try it out http://en.wikipedia.org/wiki/Washington,_D.C. If anyone could please offer some help here I would appreciate it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘世孤行 2024-07-30 12:19:01

您能否关闭检查路径中是否存在文件但允许某些扩展名通过?

routes.RouteExistingFiles = true;

// Ignore the assets directory which contains images, js, css & html
routes.IgnoreRoute("Assets/{*pathInfo}");

// Ignore text, html, files.
routes.IgnoreRoute("{file}.txt");
routes.IgnoreRoute("{file}.htm");
routes.IgnoreRoute("{file}.html");
routes.IgnoreRoute("{file}.aspx");

Could you turn of checking file exists in the routes but allow certain extensions through?

routes.RouteExistingFiles = true;

// Ignore the assets directory which contains images, js, css & html
routes.IgnoreRoute("Assets/{*pathInfo}");

// Ignore text, html, files.
routes.IgnoreRoute("{file}.txt");
routes.IgnoreRoute("{file}.htm");
routes.IgnoreRoute("{file}.html");
routes.IgnoreRoute("{file}.aspx");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文