简体   繁体   English

如何使用asp.net路由C#?

[英]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) ? 为了到达public IActionResult Get(Guid sessionGuid)应如何编写URL?

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 在我的示例中,它将是: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. 您为Controller赋予的Route("api/[controller]")属性告诉框架使用Controller的名称作为路由的一部分。

In this case you have an AuthController so all the routes in this Controller will be prefixed with api/auth/ . 在这种情况下,您具有AuthController因此该Controller中的所有路由都将以api/auth/作为前缀。

Additionally you've specified to the framework to map you Get method to the "getuser/{sessionGuid}" route (where sessionGuid is some Guid ). 另外,您已经指定了框架,以将Get方法映射到"getuser/{sessionGuid}"路由(其中sessionGuidGuid )。

Putting this together the URL you need to call is api/auth/getuser/{sessionGuid} . 将您需要调用的URL放在api/auth/getuser/{sessionGuid}

All of this needs to be prefixed by the hostname etc. http://localhost:5000/ for example. 所有这些都需要以主机名等作为前缀。例如, http://localhost:5000/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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