MVC错误行为的解决方法,缺少尾部斜杠 [英] Workaround for MVC misbehaviour with missing trailing slash

查看:195
本文介绍了MVC错误行为的解决方法,缺少尾部斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在index.cshtml中。

This is in index.cshtml.

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

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

This fails when the URL lacks a trailing slash. This URL http://host/appname/ results in expected behaviour (the browser loads /appname/App/main.js) but the same URL without a trailing slash causes the browser to attempt to load this /App/main.js resulting in a 404 not found.

我试着像这样构建它

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

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

but this only makes the problem even more obvious; the browser continues to work when the trailing slash is present, but now it throws up a message dialog full of raw HTML when the trailing slash is absent.

这是一对可能的解决方案:

Here are a couple of possible solutions:


  • 配置IIS以在到达ASP.NET之前附加尾部斜杠

  • 设置MVC应用程序以提前检查并使用尾部斜杠重定向

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

Having IIS do it is probably not the best plan. There is a high chance the configuration change will cause problems with other aspects of the system, especially for multi page (non-SPA) MVC applications. The REST habit of encoding parameters into URLs as though they were resource paths conventionally does not use a trailing slash.

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

One might argue that this notation adds nothing over conventional URL parameter encoding and violates the HTTP spec (not to mention greatly annoying yours truly) but there is considerable investment in this type of encoding and therefore server configuration is less than ideal as a solution.

推荐答案

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

To perform a "courtesy redirect" in an MVC application, open your main controller and update the Index action. In my case the app is a Durandal SPA app, so the main Index method looks like this:

public class DurandalController : Controller
{
  public ActionResult Index()
  {
    return View();
  }
}

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

We need to check the request and if necessary redirect. It ends up looking like this:

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

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

Redirection only comes into play once in the session lifecycle of a SPA app because it is loaded from the server only once per session. Implemented like this it has no consequences for other controllers and the URLs that they handle.

这篇关于MVC错误行为的解决方法,缺少尾部斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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