c# – 将global.asax迁移到ASP.NET 5

几天前,.NET Core RC1已经发布,在阅读了很多之后,我第一次放弃了,我喜欢它,但有点不同.我正在尝试将一个小型博客(内置于MVC5)迁移到MVC 6& .NET核心这并不难,但我真的很努力地重新创建了与MVC 5中完全相同的global.asax设置,ASP.NET 5不再具有global.asax,所以我无法弄清楚大部分设置的替换是?
protected void Application_Start()
    {
        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(new RazorViewEngine());

        MvcHandler.DisableMvcResponseHeader = true;
        AntiForgeryConfig.SuppressXFrameOptionsHeader = true;

        BundleConfig.RegisterBundles(BundleTable.Bundles);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

    protected void Application_BeginRequest()
    {
        Response.AddHeader("X-Frame-Options","DENY");
    }

    protected void Application_EndRequest()
    {
        if (Response.StatusCode != 301 && Response.StatusCode != 302) return;

        var targetUrl = Response.RedirectLocation.Replace("ReturnUrl","url");
        Response.RedirectLocation = targetUrl;
    }

    protected void Application_AuthenticateRequest(object sender,EventArgs e)
    {
        string typeName;

        byte userType = (byte)(Context.Request.IsAuthenticated ? byte.Parse(User.Identity.Name.Split('|')[2]) : 1);

        switch (userType)
        {
            case 1: { typeName = "Client"; break; }
            case 2: { typeName = "Admin"; break; }
            default: { typeName = "Client"; break; }
        }

        var roles = new[] { typeName };

        if (Context.User != null)
        {
            Context.User = new GenericPrincipal(Context.User.Identity,roles);
        }
    }

    private void Application_Error(object sender,EventArgs e)
    {
        Exception ex = Server.GetLastError();

        if (ex is HttpAntiForgeryException)
        {
            Response.Clear();
            Server.ClearError();
            Response.Redirect("/error/cookie",true);
        }
    }

请问,有没有办法让上述代码在MVC 6中工作,而不会留下任何设置?这对我来说是一个破坏者,谢谢你.

解决方法

根据 this blogpost Shawn Wildermuth也在一周前就有Pluralsight的网络研讨会,在那里他告诉MVC 5 global.asax,packages.config和web.config已经在ASP 5中了.所以在ASP 5中,所有从前MVC 5全局配置.asax进入新的根Startup.cs文件.

相关文章

在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题。比...
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL)。 业务流程:选择照片...
var array = new byte[4]; var i = Encoding.UTF8.GetBytes(100.ToString("x2"));//...
其实很简单,因为Combox的Item是一个K/V的object,那么就可以把它的items转换成IEnumerable<Dic...
把.net4.6安装包打包进安装程序。 关键脚本如下: 头部引用字符串对比库 !include "WordFunc....
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.NetʿreeSp...