加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:24:32 所属栏目:百科 来源:网络整理
导读:几天前,.NET Core RC1已经发布,在阅读了很多之后,我第一次放弃了,我喜欢它,但有点不同.我正在尝试将一个小型博客(内置于MVC5)迁移到MVC 6 .NET核心这并不难,但我真的很努力地重新创建了与MVC 5中完全相同的global.asax设置,ASP.NET 5不再具有global.asax,所
几天前,.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文件.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读