简体   繁体   中英

How to use asp.net routing C#?

Can anybody help me with this link?

[Route("api/[controller]")]
public class AuthController : ControllerBase
{
    private readonly IAuthService _auth;
    public AuthController(IAuthService auth)
    {
        _auth = auth;
    }

    [HttpGet("getuser/{sessionGuid}")]
    public IActionResult Get(Guid sessionGuid)
    {
        \\code         
    }

How should the URL be written in order to get to public IActionResult Get(Guid sessionGuid) ?

Move your route onto the function, and configure it however you'd like.

In my example, it would be: HTTP GET : http://api.com/my/ur/with/as/many/slashses/as/I/want/52

public class AuthController : ControllerBase
{
    private readonly IAuthService _auth;
    public AuthController(IAuthService auth)
    {
        _auth = auth;
    }

    [Route("my/url/with/as/many/slashes/as/I/want/{sessionGuid}")]
    [HttpGet("{sessionGuid}")]
    public IActionResult Get(Guid sessionGuid)
    {
        \\code         
    }
}

The Route("api/[controller]") attribute you have given your Controller tells the framework to use the name of the Controller as part of the route.

In this case you have an AuthController so all the routes in this Controller will be prefixed with api/auth/ .

Additionally you've specified to the framework to map you Get method to the "getuser/{sessionGuid}" route (where sessionGuid is some Guid ).

Putting this together the URL you need to call is api/auth/getuser/{sessionGuid} .

All of this needs to be prefixed by the hostname etc. http://localhost:5000/ for example.

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