作为在MVC的Web API一个小白就可能有很明显我错过..自定义路线只是重定向到当前页面
然而,在我ProjectController
,我有以下方法与属性(我知道这种方法应该是POST,但只是测试很容易...):
[Route("api/projects/archive/{id:int}")]
[HttpGet]
public void Archive(int id)
{
_projectService.Archive(id);
}
然而,当我打开我的网址,如:
http://localhost:49923/api/projects/archive/1
页面只是重定向到当前URL,并且不调用存档方法。我也有一个断点来验证它没有被击中。
我最好的猜测是我也必须更新我的web api路由配置这是默认的,但我只是假设路由属性是足够的?
这里是我的网页API航线配置:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {id = RouteParameter.Optional});
config.Formatters.JsonFormatter.SupportedMediaTypes
.Add(new MediaTypeHeaderValue("text/html"));
}
我在做什么错在这里? :-)
EDITS:
Clearification 1 - 我ProjectController:
public class ProjectsController : ApiController
{
private ProjectService _projectService;
public ProjectsController()
{
_projectService = new ProjectService();
}
[Route("api/projects/archive/{id:int}")]
[HttpGet]
public void Archive(int id)
{
_projectService.Archive(id);
}
}
Clearification 2 - 改:
因此,可以说我站在主页(/)。然后我转到网址“http://localhost:49923/api/projects/archive/1
”,它会重新加载页面并将其留在主页。
澄清'该页面只是重定向到当前的URL,并没有调用存档方法。“目前尚不清楚。同时显示控制器。 – Nkosi
阅读:https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api – Milney