'HttpRequestMessage in .Net 6
I'm converting my project from the .NET Framework to .NET 6, I have a library for which there is an extension method for HttpRequestMessage, now in my action controller Request.IsSet is not working as it is pointing to the namespace "Microsoft.AspNetCore.Mvc.Core", earlier in the .Net framework it was pointing to "System.Web.Http", how can I point this to the "System.Web.Http" in .NET 6, I don't see any injectors in that controller.
public IActionResult GetMessage(string type = "", int timeout = 0, int visibilitytimeout = 0)
{
if (Request.IsSet("contents")) **// Cannot Compile this code**
{
//return the message contents only
return StatusCode... ;
}
}
// Below is the extension of HttpRequestMessage class
public static bool IsSet(this HttpRequestMessage request, string queryParamName)
{
var val = request.GetQueryParamFirstValue(queryParamName) ?? "0";
return val.Equals("1") || val.Equals("true",
StringComparison.InvariantCultureIgnoreCase);
}
Solution 1:[1]
Probably the easiest way is to put the params directly into your Action.
So something like this:
public IActionResult GetMessage(string type = "", int timeout = 0, int visibilitytimeout = 0, [FromQuery] string contents)
{
if (contents is not null)
{
//return the message contents only
return StatusCode... ;
}
}
Alternatively, if you want to alter the extension method, you can do this:
public static bool IsSet(this HttpRequest request, string queryParamName)
{
return request.Query.ContainsKey(queryParamName);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
