Kemal Tezer Dilsiz Kemal Tezer Dilsiz - 1 year ago 96
ASP.NET (C#) Question

How can I replace a "Request" class method for ASP.NET Core View class?

I am trying to migrate few View classes into .NET Core from .NET and I am having an issue with the lack of "Request" class methods.

My specific code is as follows:

<form id="contactUs" method="post" action="@Request.Url.GetLeftPart(UriPartial.Authority)@Url.Action("ContactUsFormSubmit")" accept-charset="utf-8">

How can I replace the
part for .NET Core syntax?

Is there a better way of calling the authority part in .NET Core?

Answer Source

The Request can now be found as Request in the Controller (from the base class) or at @Context.Request if you are in a MVC View.

But there is no replacement for Request.Url so you need to make it yourself: (see EDIT)


But in your case it doesn't seems to be necessary. You could let the browser handle the relative path...

<form id="contactUs" method="post" action="@Url.Action("ContactUsFormSubmit")" accept-charset="utf-8">

Or use MVC Core native way (using tag helpers):

<form asp-controller="MyController" asp-action="ContactUsFormSubmit" method="post">

EDIT: Actually there is a replacement for Request.Url :

Add: using Microsoft.AspNetCore.Http.Extensions; Then call Request.GetDisplayUrl();... That will return https://localhost/MyController/MyAction?Param1=blah.