几天前,.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文件.