我有一个项目,要求我的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中有圆点?


当前回答

我相信你必须在你的web.config中设置弛豫urltofilesystemmapping属性。不久前,Haack写了一篇关于这个问题的文章(还有其他一些SO帖子也问了同样类型的问题)

<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />

编辑 从下面的评论中可以看出,. net / IIS的后续版本可能需要在系统中安装这个。网络服务器的元素。

<system.webServer>
<httpRuntime relaxedUrlToFileSystemMapping="true" />

其他回答

我相信你必须在你的web.config中设置弛豫urltofilesystemmapping属性。不久前,Haack写了一篇关于这个问题的文章(还有其他一些SO帖子也问了同样类型的问题)

<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />

编辑 从下面的评论中可以看出,. net / IIS的后续版本可能需要在系统中安装这个。网络服务器的元素。

<system.webServer>
<httpRuntime relaxedUrlToFileSystemMapping="true" />

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

在js中应该添加

btoa(parameter); 

在控制器

byte[] bytes = Convert.FromBase64String(parameter);
string parameter= Encoding.UTF8.GetString(bytes);

在一些闲逛后,我发现relaxedUrlToFileSystemMapping根本不为我工作,在我的情况下工作的是将RAMMFAR设置为true,同样适用于(.net 4.0 + mvc3)和(.net 4.5 + mvc4)。

<system.webserver>
    <modules runAllManagedModulesForAllRequests="true">

当设置RAMMFAR为true时,Hanselman发布了关于RAMMFAR和性能的帖子

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");
        }
    }
}

这就像将path="."更改为path=""一样简单。只需在web.config中删除ExensionlessUrlHandler-Integrated-4.0路径中的圆点。

这是一篇不错的文章https://weblog.west-wind.com/posts/2015/Nov/13/Serving-URLs-with-File-Extensions-in-an-ASPNET-MVC-Application