Share via

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

coder rock 436 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.

Developer technologies | C#
Developer technologies | 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.

Developer technologies | ASP.NET Core | Other
0 comments No comments

1 answer

Sort by: Most helpful
  1. AgaveJoe 31,361 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

    Was this answer helpful?

    0 comments No comments

Your answer

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