我有一个项目,要求我的url在路径上有圆点。例如,我可能有一个URL,例如www.example.com/people/michael.phelps

带有点的url会生成404。我的路由是好的。如果我传入michaelphelps,没有点,那么一切正常。如果我加一个点,就会得到404错误。示例站点运行在Windows 7和IIS8 Express上。URLScan未运行。

我尝试在我的web.config中添加以下内容:

<security>
  <requestFiltering allowDoubleEscaping="true"/>
</security>

不幸的是,这并没有什么不同。我刚刚收到一个404.0未找到错误。

这是一个MVC4项目,但我不认为这是相关的。我的路由工作得很好,我所期望的参数都在那里,直到它们包含一个点。

我需要配置什么才能在我的URL中有圆点?


当前回答

我在这个问题上卡了很长一段时间,遵循了所有不同的补救措施,但都没有效果。

我注意到,当在包含点[.]的URL末尾添加正斜杠[/]时。),它没有抛出404错误,而且它确实工作了。

我最终解决了这个问题,使用一个URL重写器,如IIS URL重写,以观察一个特定的模式,并附加训练斜杠。

我的URL看起来是这样的:/Contact/~firstname。所以我的模式很简单:/Contact/~(.*[^/])$

我从Scott Forsyth那里得到了这个想法,见下面的链接: http://weblogs.asp.net/owscott/handing-mvc-paths-with-dots-in-the-path

其他回答

我能够解决这个问题的特定版本(必须使/customer.html路由到/customer,不允许拖尾斜杠)使用https://stackoverflow.com/a/13082446/1454265的解决方案,并替换path="*.html"。

只需将此部分添加到Web。所有对路由/{*pathInfo}的请求都将由指定的处理程序处理,即使在pathInfo中有点。(取自ServiceStack MVC主机Web。配置示例和这个答案https://stackoverflow.com/a/12151501/801189)

这应该适用于iis6和iis7。你可以通过修改'add'元素中的path="*"为'route'后面的不同路径分配特定的处理程序

  <location path="route">
    <system.web>
      <httpHandlers>
        <add path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
      </httpHandlers>
    </system.web>
    <!-- Required for IIS 7.0 -->
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <validation validateIntegratedModeConfiguration="false" />
      <handlers>
        <add name="ApiURIs-ISAPI-Integrated-4.0" path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" preCondition="integratedMode,runtimeVersionv4.0" />
      </handlers>
    </system.webServer>
  </location>

作为解决方案,也可以考虑编码到不包含符号的格式。例如base64。

在js中应该添加

btoa(parameter); 

在控制器

byte[] bytes = Convert.FromBase64String(parameter);
string parameter= Encoding.UTF8.GetString(bytes);
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    [RoutePrefix("File")]
    [Route("{action=index}")]
    public class FileController : Controller
    {
        // GET: File
        public ActionResult Index()
        {
            return View();
        }

        [AllowAnonymous]
        [Route("Image/{extension?}/{filename}")]
        public ActionResult Image(string extension, string filename)
        {
            var dir = Server.MapPath("/app_data/images");

            var path = Path.Combine(dir, filename+"."+ (extension!=null?    extension:"jpg"));
           // var extension = filename.Substring(0,filename.LastIndexOf("."));

            return base.File(path, "image/jpeg");
        }
    }
}

我在这个问题上卡了很长一段时间,遵循了所有不同的补救措施,但都没有效果。

我注意到,当在包含点[.]的URL末尾添加正斜杠[/]时。),它没有抛出404错误,而且它确实工作了。

我最终解决了这个问题,使用一个URL重写器,如IIS URL重写,以观察一个特定的模式,并附加训练斜杠。

我的URL看起来是这样的:/Contact/~firstname。所以我的模式很简单:/Contact/~(.*[^/])$

我从Scott Forsyth那里得到了这个想法,见下面的链接: http://weblogs.asp.net/owscott/handing-mvc-paths-with-dots-in-the-path