Click here to Skip to main content
15,883,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a method that needs the incoming HttpRequest object. Is it possible for another method to pass their HttpRequest object to this method?

Example:
C#
public async Task<IActionResult> Index()
{
  // Can I do something like this???
  return validateRequest(Request);
}

public async Task<IActionResult> validateRequest()
{
   var myRequest = new myRequest(Request);
   // Do things to validate the request...

   return View("validatedRequest");
}


This way, hitting either www.mySite.com and www.mySite.com/validateRequest will both work.

What I have tried:

I've been searching on the internet and haven't found any samples yet.
Posted
Updated 6-Aug-20 0:08am
Comments
j snooze 5-Aug-20 17:45pm    
This may help you with what you're looking for.

https://stackoverflow.com/questions/34298318/how-to-access-the-request-object-in-an-mvc-6-controller
j snooze 5-Aug-20 17:47pm    
I realize thats mvc for .net framework, but this suggests the httpcontext.request is still the same for .net core...
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-3.1

1 solution

If the method is on the same controller, it will already have access to the same request. You don't need to pass anything to the other method.

Since you're using an async method, you will need to await the other method:
C#
public async Task<IActionResult> Index()
{
    return await validateRequest();
}

public async Task<IActionResult> validateRequest()
{
     var myRequest = new myRequest(Request);
     // Do things to validate the request...
     return View("validatedRequest");
}
Or, if the return await ... statement is the only await statement in the calling method, you can remove the async modifier from and return the other method's Task directly:
C#
public Task<IActionResult> Index()
{
    return validateRequest();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900