Can System.Web be used with ASP.Net Core with Full Framework

TheTechArch picture TheTechArch · Dec 1, 2016 · Viewed 30.1k times · Source

We are running serveral sites based on different .Net versions.

One of the sites is running .Net 4.6 and ASP.Net MVC 5.xx

To use the new syntax for Razor we want to upgrade this site to use .Net 4.6 and ASP.Net Core

We use FormsAuthentication on our sites, and we will continue using that so user can move between sites. (one of the sites is a SharePoint site running FormsAuthentication)

We understand that ASP.Net Core is not using anything from System.Web but we need to use that from controllers (to create FormsAuthentication cookie during login) and from HttpModule to verify cookie.

I have not been able to find any example how to use system.web from a site running ASP.Net Core with Full framwork. I have not been able to add dependency for system.web in project.json.

Questions.

  1. Is it possible to use system.web from a setup like this?

  2. How can we add dependency to system.web so System.Web.FormsAuthentication is available from ASP.NET MVC 6. (Controllers/ http module)

Answer

Ron C picture Ron C · Dec 1, 2016

That may not work the way you think. When you use .Net Core MVC against the full framework it's true that the System.Web namespace exists and could technically be called but it won't work like you want. The reason is that the request pipeline for Asp.Net Core and System.Web are totally different. So for example when using Asp.Net Core MVC if you check the HttpContext object from when a request comes in it will be populated with all the information you expect but if you check for the current request using System.Web objects you will see that their is no current request. That's because the current request didn't come in via the System.Web pipeline. It came in through the .Net Core "pipeline" if you will. So if you are using .Net Core MVC you will need to stick with the tools and approaches of that framework, not the ones provided in System.Web.

However, It should be possible to reference the System.Web.dll if your really want to. If you are using VS2015 you will need to get a copy of the System.Web.Dll and wrap it in a nuget package in order to establish a reference to it. See here .net core 1.0 visual studio referencing external dll VS2017RC has alpha tooling that should eliminate the need to wrap it in a NuGet package but I've had trouble installing it on my machine so I can't vouch for it personally. Update: VS2017 now has great tooling for Asp.Net Core and referencing full framework Dlls.