-1

I am migrating an existing ASP.NET Web API 2 project to ASP.NET Core. While migrating I am unable to find this.Request object. Can anyone help me solve out this issue?

smarts
  • 38
  • 6
  • [`HttpContext.Request`](https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNetCore.Http.Abstractions/HttpContext.cs#L26) ? – Pawel Jul 14 '16 at 17:25

2 Answers2

0

You need to override your class like this to get this.Request

public partial class _Default : System.Web.UI.Page
  • System.Web.UI.Page is used for web(mvc) pipeline request not for webapi – Aashish Mangal Jul 14 '16 at 10:20
  • What do you mean by **"migrating an existing WebApi2 project to .NET core?"** You want to migrate entire WebAPI logic into ASP.NET Website? or you want to consume WebAPI in your ASP.NET Web Application? And can you please clarify what do you need from `this. Request`? – Sumantha Poojary Jul 14 '16 at 10:47
  • I have an existing WEBAPI Project(service) and i am going to convert that application into .net core(Microsoft's new open source platform for .net). In my existing application i have user some code like below to get current request and their uri's.. this.Request.RequestUri.AbsoluteUri this.Request.RequestUri.Query So is there any alternative class for this? http://stackoverflow.com/users/5968562/sumantha-poojary – Aashish Mangal Jul 19 '16 at 07:35
0

This question was asked here, but since ASP.NET Core was still probably in an RC state then, I figured I'd answer here instead of referring to there because there is some stuff that's obsolete or completely gone from the official release.

Assuming your controller class inherits from Controller (or more specifically, ControllerBase) then it does have a this.Request property as you can see here and here. As Pawel noted, you can also access it from the this.HttpContext property.

The request's URL is broken up into several properties on HttpRequest. You can access the URL in a friendlier API by adding using Microsoft.AspNetCore.Http.Extensions; which gets you access to the following extension methods:

  • GetDisplayUrl()
  • GetEncodedUrl()

As far as the query string, HttpRequest provides QueryString and Query properties for you to interact with.

Side note: I just created an app from scratch targeting ASP.NET Core on .NET Core for the first time on this laptop, and it took a while for the Intellisense to work for the Request property, so I'm wondering if that could have been your issue.

smarts
  • 38
  • 6