自等于MVC 404等页面跳转

using System;
using System.Web.Mvc;

namespace Palmmedia.Common.Net.Mvc
{
    /// <summary>
    /// <see cref="System.Web.Mvc.ViewResult"/> which renders a 404-Error with a view.
    /// </summary>
    public class HttpNotFoundWithViewResult : ViewResult
    {
        /// <summary>
        /// HTTP 404 is the status code for Not Found
        /// </summary>
        private const int NotFoundCode = 404;

        /// <summary>
        /// Initializes a new instance of the <see cref="HttpNotFoundWithViewResult"/> class.
        /// </summary>
        public HttpNotFoundWithViewResult()
            : this(null, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="HttpNotFoundWithViewResult"/> class.
        /// </summary>
        /// <param name="viewName">Name of the view.</param>
        public HttpNotFoundWithViewResult(string viewName)
            : this(viewName, null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="HttpNotFoundWithViewResult"/> class.
        /// </summary>
        /// <param name="viewName">Name of the view.</param>
        /// <param name="statusDescription">The status description.</param>
        public HttpNotFoundWithViewResult(string viewName, string statusDescription)
            : base()
        {
            if (viewName == null)
            {
                throw new ArgumentNullException("viewName");
            }

            this.ViewName = viewName;
            this.StatusDescription = statusDescription;
        }

        /// <summary>
        /// Gets the status description.
        /// </summary>
        public string StatusDescription { get; private set; }

        /// <summary>
        /// When called by the action invoker, renders the view to the response.
        /// </summary>
        /// <param name="context">The context that the result is executed in.</param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="context"/> parameter is null.</exception>
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.StatusCode = NotFoundCode;
            if (this.StatusDescription != null)
            {
                context.HttpContext.Response.StatusDescription = this.StatusDescription;
            }

            base.ExecuteResult(context);
        }
    }
}

控制器调用:

if (entry == null)
            {
                return new HttpNotFoundWithViewResult(MVC.Shared.Views.NotFound);    //public readonly string NotFound = "~/Views/Shared/NotFound.cshtml";
            }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值