【问题标题】:401 code not handled in mvc project properly401 代码未在 mvc 项目中正确处理
【发布时间】:2014-11-24 20:35:29
【问题描述】:

我有带有自定义AuthorizeAttribute 的 ASP.NET MVC4 项目来控制授权。为了更容易解释我的情况,我创建了一个虚拟项目来说明问题。

我只有一个控制器

using System.Web.Mvc;
using MvcApplication2.Helper;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new ViewModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(ViewModel model)
        {
            Session["ModelValue"] = model.InputValue;

            return RedirectToAction("Step2");
        }

        [MyAuthorize]
        public ActionResult Step2()
        {
            return View();
        }
    }
}

目的很简单,从Index 我接受一些输入,将值存储在会话中并重定向到Step2。我在第 2 步有我的授权属性。我的属性的代码是

using System;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Helper
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
    public class MyAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext.Session["ModelValue"] == null)
            {
                return false;
            }
            else
            {
                string inputValue = httpContext.Session["ModelValue"] as string;

                if (inputValue != "1")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
    }
}

目的很简单,检查session值是否为1。

运行应用程序,在文本框中输入 1 并看到 step2 视图,如果输入其他任何内容,则会得到默认的 401.0 页面。

现在我打开了web.config 文件以包含

  <system.web>
    <customErrors mode="On" defaultRedirect="~/Error">
      <error statusCode="401" redirect="~/401.htm" />
    </customErrors>

    <compilation debug="true" targetFramework="4.0" />

  </system.web>

我希望当应用程序捕获 401 代码时,我应该看到 401.htm 的内容。但现实情况是,我仍然从服务器获得默认的 401 错误显示。

知道为什么吗?

【问题讨论】:

标签: asp.net-mvc-4 http-status-code-401


【解决方案1】:

另外使用这个:

      <system.webServer>
        <httpErrors>
          <error statusCode="401" path="~/Home/Login"/>
          <error statusCode="404" path="~/Error/NotFound"/>
        </httpErrors>
</system.webServer>

【讨论】:

  • 我似乎没有工作,我在 web.cofnig 中添加了&lt;httpErrors&gt; &lt;remove statusCode="401" subStatusCode="-1" /&gt; &lt;error statusCode="401" path="~/401.htm" /&gt; &lt;/httpErrors&gt;,它仍然在我的 IIS Express 8.5 中显示默认系统 401 页面
  • 我看不到您的代码,但我只能建议您确保 httpErrors 在 system.webServer 标记中。还可以考虑使用控制器/操作路径而不是 htm 页面,并且您不需要删除标签。请让我知道它是否有效。
  • 我已经在system.webServer 中包含了httpErrors,它没有任何区别。关于配置文件中的静态文件或 MVC 控制器/动作,唯一的区别是要加载哪个,它不应该影响功能,对吗?
  • 老实说,我从来没有尝试过使用静态文件,但是应该很容易找出这是否是造成差异的原因。只需将其更改为任何控制器\操作,看看它是否有效。也不要使用删除标签,因为它会删除新的错误标签。
猜你喜欢
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多