简体   繁体   中英

What is the difference between using Route and without in Web Api Controller?

I have this code in Web Api controller in C#. One is with Route attribute and another is without.

    [HttpGet]
    [Route("GetReport")]
    public async Task<IActionResult> GetReport(){
      // code removed for brevity
    }

and another version is

    [HttpGet("GetReport")]
    public async Task<IActionResult> GetReport(){
      // code removed for brevity
    }

They both works the same, is it?

They both works the same, is it?

Yes, though you might prefer to just use the HttpGet("...") as it's neater to use one attribute instead of two. Personally I don't use Route, and I fully specify all routes in a HttpXxx, purely because finding the relevant code when some/page/url/blah is crashing, is a matter of doing Ctrl-F for the route as presented in the browser address bar (minus any parameter values). That's a lot harder to do if your route is broken up across RoutePrefix/Route

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM