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


当前回答

这是我在iis7.5和. net Framework 4.5环境中发现的404错误的最佳解决方案,并且不使用:runAllManagedModulesForAllRequests="true"。

我关注了这个帖子:https://forums.asp.net/t/2070064.aspx?Web+API+2+URL+routing+404+error+on+IIS+7+5+IIS+Express+works+fine,我已经修改了我的网站。现在MVC web应用程序在iis7.5和. net Framework 4.5环境下工作得很好。

其他回答

作为解决方案,也可以考虑编码到不包含符号的格式。例如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");
        }
    }
}

MVC 5.0解决方案。

许多建议的答案似乎在MVC 5.0中不起作用。

由于上一节中的404点问题可以通过结尾的斜杠来解决,下面是我使用的小技巧,干净而简单。

同时在视图中保持一个方便的占位符:

@Html.ActionLink("Change your Town", "Manage", "GeoData", new { id = User.Identity.Name }, null)

添加一点jquery/javascript来完成工作:

<script>
    $('a:contains("Change your Town")').on("click", function (event) {
        event.preventDefault();
        window.location.href = '@Url.Action("Manage", "GeoData", new { id = User.Identity.Name })' + "/";
    });</script>

请注意后面的斜杠,它负责更改

http://localhost:51003/GeoData/Manage/user@foo.com

http://localhost:51003/GeoData/Manage/user@foo.com/

对于那些只在一个网页上有这个的人来说,超级简单的答案。编辑actionlink并在它的末尾加上+“/”。

  @Html.ActionLink("Edit", "Edit", new { id = item.name + "/" }) |

是否可以更改URL结构? 为了我的工作,我尝试了一条路线

url: "Download/{fileName}"

但它对任何有。在里面。

我把路线换成了

    routes.MapRoute(
        name: "Download",
        url:  "{fileName}/Download",
        defaults: new { controller = "Home", action = "Download", }
    );

现在我可以输入localhost:xxxxx/File1.doc/Download,它可以正常工作。

我在视图中的助手也注意到了这一点

     @Html.ActionLink("click here", "Download", new { fileName = "File1.doc"})

也会生成到localhost:xxxxx/File1.doc/Download格式的链接。

也许你可以在路由的末尾放一个不需要的单词,比如“/view”或action,这样你的属性就可以以/结尾的/结尾,比如/mike.smith/view