c# - ASP.NET 5 中的路由前缀

标签 c# asp.net

我有一个名为 BeconsController 的 Controller ,它有一个简单的获取操作。使用路线:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "Default",
        template: "{controller}/{action}/{id?}");
});

路由很好,http://locahost/beacons/5(其中 5 是 id)解析为正确的操作。但是,我希望它位于“api”路径下。我原以为这可以通过写作来完成:

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "Default",
        template: "api/{controller}/{action}/{id?}");
});

但是当我将浏览器指向 http://locahost/api/beacons/5 时,我收到了 404。事实上,旧路径也仍然有效:http://locahost/beacons/5 将我带到页面!

这应该很简单。我做错了什么?

最佳答案

您是只使用这条路线还是在路线对象中有几条路线?如果您有多个,它将​​采用与其匹配的第一条路线。

也许以下链接会对您有所帮助: http://aspnetmvc.readthedocs.org/projects/mvc/en/latest/migration/migratingfromwebapi2.html http://docs.asp.net/en/latest/fundamentals/routing.html?highlight=routes

关于c# - ASP.NET 5 中的路由前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36661301/

相关文章:

c# - 泛型:其中 T 是 TypeA 或 TypeB

c# - 如何将我的 python 模块及其依赖项(例如 Numpy/Scipy)打包到一个文件中,例如用于 C# 调用的 dll?

javascript - 通过 jquery 禁用按钮

c# - DB2 iSeries AS400 - 数据库连接

c# - 绑定(bind)运行次数可变的 RichTextBox

c# - ViewModel 中的 HTML.Raw 等效 DataAnnotation

c# - 从多个线程调用 LINQ-to-SQL 中的 SubmitChanges()

asp.net - Global.asax 中 Application_End 处理程序内的 Server.MapPath

c# - Paypal IPN delay-is sleeping 站点解决方案

c# - asp.net c# 中的日历控件