如何使用带有点/句点的基于MVC5属性的路由? [英] How can I use MVC5 attribute-based routes with a dot/period in them?

查看:175
本文介绍了如何使用带有点/句点的基于MVC5属性的路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有一个现成的MVC 5.2应用程序,启用了基于属性的路由(从提供的RouteConfig.RegisterRoutes(RouteCollection routes)方法调用routes.MapMvcAttributeRoutes();).

I have basically an out-of-the box MVC 5.2 app, with attribute-based routes enabled (calling routes.MapMvcAttributeRoutes(); from the provided RouteConfig.RegisterRoutes(RouteCollection routes) method).

以下内容位于HomeController中:

The following is in HomeController:

    [Route("hello.txt")]
    [Route("hellotxt-testing")]
    public ActionResult ShowFile()
    {
        return Content("Hello world");
    }

我可以成功访问/hellotxt-testing(证明基于属性的路由正常工作):

I can successfully access /hellotxt-testing (proving attribute-based routing is working correctly):

但是如果我访问/hello.txt,我会得到一个404页面:

But I get a 404 page if I access /hello.txt:

与进入/hello404时的情况相反,在这里我获得了另一个404页面:

In contrast to when I go to /hello404, where I get a different 404 page:

我猜想第二个404页面来自ASP.NET,而第一个页面来自IIS,IIS甚至没有将请求传递给ASP.NET?

I am guessing that the second 404 page is from ASP.NET, while the first is from IIS which isn't even passing the request to ASP.NET?

我该怎么做才能确保ASP.NET可以确保这些URL中带有句点?还有其他特殊"字符也会引起问题吗?

What do I do to ensure that I can ensure ASP.NET gets these URLs that have periods in them? Are there other 'special' characters that would also cause a problem?

推荐答案

看到的错误之间的差异是因为,当您尝试访问/hello.txt时,IIS会尝试访问位于服务器上的静态资源文件. /hello404只是一条未知路线,因此抛出404.

The difference between errors you see is because when you try to access /hello.txt IIS tries to reach the static resource file located on the server, on the other hand /hello404 is just an unknown route so 404 is thrown.

为确保我是对的,请尝试在末尾添加斜杠并访问/hello.txt/,现在应该出现404错误.

To ensure that I am right try to add slash at the end and access /hello.txt/ you should now get 404 error.

有几种方法可以解决此问题:

There are several ways to resolve this problem:

首先,您可以尝试

<modules runAllManagedModulesForAllRequests="true">

,但此选项称为RAMMFAR 会降低Web应用程序的性能.后果.

but this option called RAMMFAR hurts the performance of web application. and has other consequences.

第二,您可以为IIS URL REWRITE创建规则,以

Second you can create rule for IIS URL REWRITE to always add trailing slash to each incoming URL's

第三

但是您不喜欢这样.

第四尝试创建自定义的http处理程序以在所有请求中添加斜杠,例如本主题中的

Fourth try to create custom http handler to add trailing slash to all requests like in this topic Add a trailing slash at the end of each url?

如果您需要在应用程序的许多地方使用它,我会选择 second 选项.

I would go with second option if you need this to work in many places in your application.

但是我认为最好的是第三.带有点的此类网址对用户不友好,并且违反了SEO规则.您应该在公开的网站上避免使用它们.因此,假设您只需要一个控制器并路由,则该选项是最快,最干净的一个.

But I think the best one is third. Such url's with dots are not user friendly and are against SEO rules. You should avoid them in publicly available websites. So assuming you need this for only one controller and route this option is the fastest and cleanest one.

这篇关于如何使用带有点/句点的基于MVC5属性的路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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