HttpGet 操作不会被调用
HttpGet Action does not get invoked
Startup.cs,样板:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
我有一个控制器 class,MembersController。
[Produces("application/json")]
[Route("api/Members")]
public class MembersController : Controller
{
[HttpGet("{email},{password}")]
[Route("api/members/authenticate/")]
public async void Authenticate(String email, String password)
{
///the method that won't fire
}
// GET: api/Members/5
[HttpGet("{id}")]
public async Task<IActionResult> GetMember([FromRoute] int id)
{
///the boiler plate method that gets called
}
}
基本上我尝试添加一个方法,Authenticate
,我在其中使用 username
和 password
。我设置了一个路由和一些 HTTPGet 参数。但无论我如何处理它(例如前往 http://localhost:64880/api/members/authenticate/
),我都无法调用添加的 Authenticate 方法。
我想这是一个路由的东西?
您正在混合路线。
鉴于您已将控制器装饰为 [Route("api/Members")]
作为路由前缀,然后将操作装饰为 [Route("api/members/authenticate/")]
,该操作的最终路由将是 api/Members/api/members/authenticate/
。看到您尝试调用的内容有何不同?
通常您会希望为身份验证操作执行 POST 以允许将参数发送到请求正文中的操作。
创建一个模型来保存数据
public class AuthModel {
public string email { get; set; }
public string password { get; set; }
}
接下来修复路线。您似乎在使用属性路由,还混合了路由属性和 http 动词属性的路由模板。
来自评论
Also, that [Produces("application/json")]
is completely pointless since JSON is the default
[Route("api/Members")]
public class MembersController : Controller {
//Matches POST api/members/authenticate
[HttpPost("authenticate")]
public async Task<IActionResult> Authenticate([FromBody] AuthModel model) {
String email = model.email;
String password = model.password
//fake async task
await Task.Delay(1);
return Ok();
}
// GET: api/Members/5
[HttpGet("{id}")]
public async Task<IActionResult> GetMember([FromRoute] int id) {
///the boiler plate method that gets called
//fake async task
await Task.Delay(1);
return Ok();
}
}
Startup.cs,样板:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
我有一个控制器 class,MembersController。
[Produces("application/json")]
[Route("api/Members")]
public class MembersController : Controller
{
[HttpGet("{email},{password}")]
[Route("api/members/authenticate/")]
public async void Authenticate(String email, String password)
{
///the method that won't fire
}
// GET: api/Members/5
[HttpGet("{id}")]
public async Task<IActionResult> GetMember([FromRoute] int id)
{
///the boiler plate method that gets called
}
}
基本上我尝试添加一个方法,Authenticate
,我在其中使用 username
和 password
。我设置了一个路由和一些 HTTPGet 参数。但无论我如何处理它(例如前往 http://localhost:64880/api/members/authenticate/
),我都无法调用添加的 Authenticate 方法。
我想这是一个路由的东西?
您正在混合路线。
鉴于您已将控制器装饰为 [Route("api/Members")]
作为路由前缀,然后将操作装饰为 [Route("api/members/authenticate/")]
,该操作的最终路由将是 api/Members/api/members/authenticate/
。看到您尝试调用的内容有何不同?
通常您会希望为身份验证操作执行 POST 以允许将参数发送到请求正文中的操作。
创建一个模型来保存数据
public class AuthModel {
public string email { get; set; }
public string password { get; set; }
}
接下来修复路线。您似乎在使用属性路由,还混合了路由属性和 http 动词属性的路由模板。
来自评论
Also, that
[Produces("application/json")]
is completely pointless since JSON is the default
[Route("api/Members")]
public class MembersController : Controller {
//Matches POST api/members/authenticate
[HttpPost("authenticate")]
public async Task<IActionResult> Authenticate([FromBody] AuthModel model) {
String email = model.email;
String password = model.password
//fake async task
await Task.Delay(1);
return Ok();
}
// GET: api/Members/5
[HttpGet("{id}")]
public async Task<IActionResult> GetMember([FromRoute] int id) {
///the boiler plate method that gets called
//fake async task
await Task.Delay(1);
return Ok();
}
}