Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'HttpRequest' does not contain a definition for 'Params'

I'm using .NET Core 2.0.2 to create an ASP.NET webapp in C#.

Everytime I use Request.Url in my controllers, dotnet run outputs an error:

error CS1061: 'HttpRequest' does not contain a definition for 'Url' and no extension method 'Url' accepting a first argument of type 'HttpRequest' could be found

The same thing happens with Request.Params. Even though the .NET documentation says there is a getter for the Params property.

I managed to find a workaround for Request.Url: I use the Request.GetUri() method. However I couldn't find such a replacement for Request.Params.

Here are my using statements:

using System;
using System.Diagnostics;
using area.Models;
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.AspNetCore.Mvc;
using Tweetinvi;
using Tweetinvi.Models;

Why am I getting those errors? Do you know a fix? Otherwise, do you have a workaround to get Request.Params?

like image 206
Ronan Boiteau Avatar asked Jan 30 '23 03:01

Ronan Boiteau


2 Answers

The stuff you used to get from generic request params is accessible in specific properties of Request like Query, Cookies, Form etc.

If you are trying to access a querystring item value, you may use the Request.Query property.

var v = Request.Query["oauth_verifier"];

You can use the TryGetValue method on the collection to get the value if it exist.

if (Request.Query.TryGetValue("oauth_verifier",out StringValues val))
{
    var theValue = val[0];
    var orUseThis = val.ToString();
    // Use theValue as needed
}
like image 83
Shyju Avatar answered Feb 01 '23 17:02

Shyju


HttpRequest class is defined (in different ways) in System.Web and in Microsoft.AspNetCore.Http namespaces. The former has Params property the latter doesn't. Debug to find what kind HttpRequest you have (and add proper using...).

like image 36
Alex Kudryashev Avatar answered Feb 01 '23 18:02

Alex Kudryashev