How to validate Query String url before execution of JsonResult or ActionResult of mvc5

coder rock 396 Reputation points
2021-09-27T20:04:17.657+00:00

Below is my code of MVC JsonResult method

 [HttpGet]
  //[HttpGet("user/register/{OrgId:int}/{dealerId:int}")] 
  public JsonResult register(int? OrgId, int dealerId)
   {
     //my code
   }

I am trying if query string value is not there means handle the error to not go yellow page above i have tried commented line but this is not working for me to validate url have query string values.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,539 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,108 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 28,786 Reputation points
    2021-09-27T21:44:22.013+00:00

    Use a nullable type for each input parameter and use a unique route.

    [HttpGet("getbyOrgIdandDealerId")]
    public IActionResult Get(int? OrgId, int? DealerId)
    {
        return Ok(new { orgId = OrgId, dealerId = DealerId });
    }
    

    URLs

    https://localhost:44352/api/valuesgetbyOrgIdandDealerId?OrgId=1&DealerId=2
    https://localhost:44352/api/values/getbyOrgIdandDealerId

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.