ASP.net 核心如何映射 post 并将请求路由到 HTTP://site/area/controller
ASP.net core how to map post and put request route to HTTP://site/area/controller
我要映射我的 post 并向 URL http://site/area/controller
提出请求,
我使用了 HttpPost
和 HttpPut
属性。
但是当我post数据到服务器时,它返回404 Not found。
我的代码:
public class ApplicationsController : Controller
{
private readonly IApplicationService _applicationService;
public ApplicationsController(IApplicationService applicationService)
{
_applicationService = applicationService;
}
[HttpGet]
public async Task<IActionResult> Index(ApplicationQuery query)
{
var permissionNodes = await _applicationService.SelectePagedApplicationsAsync(query);
ViewData["Query"] = query;
return View(permissionNodes.Data);
}
[HttpPost("/Applications")]
public async Task<IActionResult> Create(ApplicationViewModel model)
{
var app = Mapper.Map<Application>(model);
var result = await _applicationService.CreateApplicationAsync(app);
ViewData["ServiceResult"] = result;
return View(nameof(Edit));
}
[HttpPut("/Applications")]
public async Task<IActionResult> Update(ApplicationViewModel model)
{
var app = Mapper.Map<Application>(model);
var result = await _applicationService.CreateApplicationAsync(app);
ViewData["ServiceResult"] = result;
return View(nameof(Edit));
}
[HttpGet]
public async Task<IActionResult> Edit(long? id)
{
var result = await _applicationService.FindApplicationAsync(new ApplicationQuery
{
Id = id
});
var model = Mapper.Map<ApplicationViewModel>(result.Data);
return View(model);
}
让 MVC 将我的请求映射到我的操作的方法是什么?
您似乎缺少相关路由的令牌分配。
Token replacement in route templates ([controller], [action], [area])
For convenience, attribute routes support token replacement by enclosing a token in square-braces ([
, ]
). The tokens [action]
, [area]
, and [controller]
will be replaced with the values of the action name, area name, and controller name from the action where the route is defined.
鉴于您希望将这些路由隔离到仅 POST
和 PUT
端点,因此相应地更新路由
public class ApplicationsController : Controller {
//...code removed for brevity
//Matches POST /{area}/applications
[HttpPost("[area]/[controller]")]
public async Task<IActionResult> Create([FromBody]ApplicationViewModel model) {
//...code removed for brevity
}
//Matches PUT /{area}/applications
[HttpPut("[area]/[controller]")]
public async Task<IActionResult> Update([FromBody]ApplicationViewModel model) {
//...code removed for brevity
}
//...code removed for brevity
}
以上还假设已经对区域进行了适当的配置
我要映射我的 post 并向 URL http://site/area/controller
提出请求,
我使用了 HttpPost
和 HttpPut
属性。
但是当我post数据到服务器时,它返回404 Not found。
我的代码:
public class ApplicationsController : Controller
{
private readonly IApplicationService _applicationService;
public ApplicationsController(IApplicationService applicationService)
{
_applicationService = applicationService;
}
[HttpGet]
public async Task<IActionResult> Index(ApplicationQuery query)
{
var permissionNodes = await _applicationService.SelectePagedApplicationsAsync(query);
ViewData["Query"] = query;
return View(permissionNodes.Data);
}
[HttpPost("/Applications")]
public async Task<IActionResult> Create(ApplicationViewModel model)
{
var app = Mapper.Map<Application>(model);
var result = await _applicationService.CreateApplicationAsync(app);
ViewData["ServiceResult"] = result;
return View(nameof(Edit));
}
[HttpPut("/Applications")]
public async Task<IActionResult> Update(ApplicationViewModel model)
{
var app = Mapper.Map<Application>(model);
var result = await _applicationService.CreateApplicationAsync(app);
ViewData["ServiceResult"] = result;
return View(nameof(Edit));
}
[HttpGet]
public async Task<IActionResult> Edit(long? id)
{
var result = await _applicationService.FindApplicationAsync(new ApplicationQuery
{
Id = id
});
var model = Mapper.Map<ApplicationViewModel>(result.Data);
return View(model);
}
让 MVC 将我的请求映射到我的操作的方法是什么?
您似乎缺少相关路由的令牌分配。
Token replacement in route templates ([controller], [action], [area])
For convenience, attribute routes support token replacement by enclosing a token in square-braces (
[
,]
). The tokens[action]
,[area]
, and[controller]
will be replaced with the values of the action name, area name, and controller name from the action where the route is defined.
鉴于您希望将这些路由隔离到仅 POST
和 PUT
端点,因此相应地更新路由
public class ApplicationsController : Controller {
//...code removed for brevity
//Matches POST /{area}/applications
[HttpPost("[area]/[controller]")]
public async Task<IActionResult> Create([FromBody]ApplicationViewModel model) {
//...code removed for brevity
}
//Matches PUT /{area}/applications
[HttpPut("[area]/[controller]")]
public async Task<IActionResult> Update([FromBody]ApplicationViewModel model) {
//...code removed for brevity
}
//...code removed for brevity
}
以上还假设已经对区域进行了适当的配置