Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the ASP.NET Core MVC equivalent to Request.RequestURI?

I found a blog post that shows how to "shim" familiar things like HttpResponseMessage back into ASP.NET Core MVC, but I want to know what's the new native way to do the same thing as the following code in a REST Post method in a Controller:

// POST audit/values [HttpPost] public System.Net.Http.HttpResponseMessage Post([FromBody]string value) {     var NewEntity = _repository.InsertFromString(value);      var msg = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Created);     msg.Headers.Location = new Uri(Request.RequestUri + NewEntity.ID.ToString());     return msg;  } 

In an ASP.NET Core MVC project, I can't seem to get Request.RequestUri.

I tried inspecting Request, and I was able to make a function like this:

private string UriStr(HttpRequest Request) {     return Request.Scheme + "://" + Request.Host + Request.Path; // Request.Path has leading / } 

So I could write UriStr(Request) instead. But I'm not sure that's right. I feel like I'm hacking my way around, and not using this correctly.

A related question for earlier non-Core ASP.NET MVC versions asks how to get the base url of the site.

like image 530
Warren P Avatar asked Jul 24 '15 18:07

Warren P


People also ask

What is request in ASP.NET MVC?

by Microsoft. Learn how the ASP.NET MVC framework processes a browser request step-by-step. Requests to an ASP.NET MVC-based Web application first pass through the UrlRoutingModule object, which is an HTTP module. This module parses the request and performs route selection.

Is ASP.NET Core same as MVC?

The main difference between ASP.NET Core and ASP.NET MVC 5 is their cross-platform approach. ASP.NET Core can be used on Windows, Mac, or Linux, whereas ASP.NET MVC 5 can only be used for applications on Windows. The ASP.NET Core MVC is a framework for building web apps and APIs, optimized for use with ASP.NET Core.

What is request delegate in ASP.NET Core?

The request delegates handle each HTTP request. Request delegates are configured using Run, Map, and Use extension methods. An individual request delegate can be specified in-line as an anonymous method (called in-line middleware), or it can be defined in a reusable class.


1 Answers

Personally, I use :

new Uri(request.GetDisplayUrl()) 
  • GetDisplayUrl fully un-escaped form (except for the QueryString)
  • GetEncodedUrl - fully escaped form suitable for use in HTTP headers

These are extension method from the following namespace : Microsoft.AspNetCore.Http.Extensions

like image 185
t.ouvre Avatar answered Oct 09 '22 05:10

t.ouvre