【问题标题】:url.action method not resulting in expected URL outputurl.action 方法没有产生预期的 URL 输出
【发布时间】:2016-10-03 20:55:47
【问题描述】:

我在一个示例 MVC 项目中工作,我使用了下面的 url 操作方法,但它没有按预期进入 URL。

我总共有两个视图 - createindex 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


【解决方案1】:

编译代码后,

 $("#btnCancel").click(function () {
    window.location = '@Url.Action("Index", "Person")';
});

变成这样:

但是编译代码后,

 $("#btnCancel").click(function () {
    window.location = '@Url.Action("create", "Person")';
});

变成这样:

【讨论】:

  • 我可以理解解释,但是人员/索引页面的 URL 的原因是从 URL 中跳过索引。是不是因为路由配置的原因。
  • 是的,我猜这是因为默认的 route.config 配置。我更改了视图的名称。它工作正常。 :-)
  • ali, thnQ 非常感谢,请您确认这是不是因为这个路由配置。?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-13
相关资源
最近更新 更多