Kemal Tezer Dilsiz
Kemal Tezer Dilsiz

Reputation: 4009

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 Request.Url.GetLeftPart(UriPartial.Authority) part for .NET Core syntax?

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

Upvotes: 2

Views: 854

Answers (1)

Gerardo Grignoli
Gerardo Grignoli

Reputation: 15207

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)

string.Concat(Request.Scheme, 
    "://", 
    Request.Host.ToUriComponent(), 
    Request.PathBase.ToUriComponent(), 
    Request.Path.ToUriComponent(),
    Request.QueryString.ToUriComponent())

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.

Upvotes: 2

Related Questions