【发布时间】:2016-10-03 20:55:47
【问题描述】:
我在一个示例 MVC 项目中工作,我使用了下面的 url 操作方法,但它没有按预期进入 URL。
我总共有两个视图 - create 和 index of person entity。 像这样的网址; app/Person/Create & app/Person/Index。
这里的人是控制器,它被创建为类名:PersonController
当用户点击取消按钮时,现在我正在尝试将索引视图加载回用户。
$("#btnCancel").click(function () {
window.location = '@Url.Action("Index", "Person")';
});
在个人控制器中,我在名称索引中定义了操作。
但 url 操作将 url 返回为:- app/person
同样调用这个 url 动作会在 person coltroller 类的动作 Index 处命中断点。我期待像 app/Person/Create & app/Person/Index 这样的 URL。
Route config.cs - 默认是 mvc 项目:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
有人可以帮忙解决这个问题吗?
【问题讨论】:
-
您希望获得的预期网址是什么?
-
显示您的
RouteConfig.cs文件。 -
索引很可能是您在路由配置文件中的默认操作。因此索引不会显示在 url 但仍应正确映射。
-
我已经添加了路由配置代码 - 与 mvc 项目附带的默认代码相同。
-
是的,我猜是因为默认的 route.config 配置(我不确定)。我更改了视图的名称。它工作正常。 :-)
标签: jquery asp.net-mvc asp.net-mvc-4 razor url.action