I am not seeing much information on porting a asp.net mvc 4.6 application to the asp.net core 2.1 Tons of 1.0 to 2.0 and 2.0. to 2.1 articles, but it is a bit steep in changes.
Ok, I have been all over google and stackoverflow and what I am trying/wanting to do is convert an older .net application and bring it into .net core.
Some big culprits I see as problems are the following:
HttpContext.Current
HttpContext.Current.Request.Url.AbsolutePath
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)
private static string CollectionToHtmlTable(HttpCookieCollection collection)
I was seeing these articles which are helpful, but much to be desired.
https://www.carlrippon.com/httpcontext-in-asp-net-core/ https://dotnetcoretutorials.com/2017/01/05/accessing-httpcontext-asp-net-core/
Inside Controller it seems the easiest, but most of this HttpContext.Current
stuff is sprinkled around various places domain libraries etc..
So in both url's above it seems that using this line below in the startup.cs
however, I don't have a startup.cs outside of main asp.net core
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
Then also in startup I seen this article https://www.strathweb.com/2016/12/accessing-httpcontext-outside-of-framework-components-in-asp-net-core/
//one of many things added to project
services.AddHttpContextAccessor();
I realize that several questions have been asked in the last couple of years - here is to hoping that some brave soul has explored this stuff and has some advise/answers to how to migrate this stuff over.
Method to Port: #1
public Service(string key, LogRecord.CallType callType, string operation, string opArg, string opResponse)
{
this.Key = string.IsNullOrEmpty(key)
? HttpContext.Current != null ? "Inbound/WebHttp: " + HttpContext.Current.Request.Url.AbsolutePath : string.Empty
: key;
this.CallType = Enum.GetName(typeof(LogRecord.CallType), callType);
this.URL = HttpContext.Current != null && callType == LogRecord.CallType.WebHttp
? HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)
: string.Empty;
this.Operation = operation;
this.OpArg = opArg;
this.OpResponse = opResponse;
}
Another: #2
private static string CollectionToHtmlTable(HttpCookieCollection collection)
{
// Converts HttpCookieCollection to NameValueCollection
var nvc = new NameValueCollection();
foreach (string item in collection)
{
var httpCookie = collection[item];
if (httpCookie != null)
{
nvc.Add(item, httpCookie.Value);
}
}
return CollectionToHtmlTable(nvc);
}