具有缺少尾部斜杠的MVC错误行为的解决方法

Pet*_*one 4 iis requirejs asp.net-mvc-4

这是在index.cshtml中.

<script>alert(window.location.href);</script>
<script 
  type="text/javascript" 
  src="~/Scripts/require.js" 
  data-main="App/main">
</script>
Run Code Online (Sandbox Code Playgroud)

当URL缺少尾部斜杠时,这会失败.此URL http://host/appname/导致预期的行为(浏览器加载/appname/App/main.js),但没有尾部斜杠的相同URL导致浏览器尝试加载此/App/main.js,导致找不到404.

我试着像这样构建它

<script>alert(window.location.href);</script>
<script 
  type="text/javascript" 
  src="~/Scripts/require.js" 
  data-main="@Url.Content("~/App/main")">
</script>
Run Code Online (Sandbox Code Playgroud)

但这只会使问题更加明显; 当尾部斜杠存在时,浏览器继续工作,但现在当没有尾部斜杠时,它会抛出一个充满原始HTML的消息对话框.

以下是一些可能的解决方案:

  • 配置IIS以在到达ASP.NET之前附加尾部斜杠
  • 设置MVC应用程序以提前检查并使用尾部斜杠重定向

有IIS做它可能不是最好的计划.配置更改很可能会导致系统其他方面出现问题,尤其是对于多页(非SPA)MVC应用程序.将参数编码到URL中的REST习惯就好像它们是资源路径一样,通常不使用尾部斜杠.

有人可能会争辩说,这种符号比传统的URL参数编码没有增加任何内容并且违反了HTTP规范(更不用说真正令你烦恼的了),但是这种类型的编码投入相当大,因此服务器配置不太理想.

Pet*_*one 5

要在MVC应用程序中执行"礼貌重定向",请打开主控制器并更新"索引"操作.在我的情况下,该应用程序是一个Durandal SPA应用程序,因此主要的索引方法如下所示:

public class DurandalController : Controller
{
  public ActionResult Index()
  {
    return View();
  }
}
Run Code Online (Sandbox Code Playgroud)

我们需要检查请求并在必要时重定向.最终看起来像这样:

public class DurandalController : Controller
{
  public ActionResult Index()
  {
    var root = VirtualPathUtility.ToAbsolute("~/");
    if ((root != Request.ApplicationPath) && (Request.ApplicationPath == Request.Path))
      return Redirect(root + "#");
    else
      return View();
  }
}
Run Code Online (Sandbox Code Playgroud)

重定向仅在SPA应用程序的会话生命周期中发挥作用一次,因为每个会话仅从服务器加载一次.像这样实现它对其他控制器及其处理的URL没有任何影响.