【问题标题】:Removing a controller name from a URL in MVC4从 MVC4 中的 URL 中删除控制器名称
【发布时间】:2013-04-14 11:31:21
【问题描述】:

我正在动态构建 URL 以传递给 jQuery 按钮单击事件处理程序。

URL 来自模态窗口定义:

<div class="modal hide fade in" id="modal-view-tenant-profile" 
data-url="Tenants/ViewProfile">...</div>

id 来自一个按钮:

<button class="btn btn-primary btn-block show-modal" 
data-id="@result.UserId">View profile</button>

在我的 JS 函数中,我抓取两条数据

var url = "Tenants/ViewProfile";
var id = $(this).attr('data-id');

然后像$.get(url + '/' + id, function (data))...这样构造URL

要正常工作,URL 应如下所示:

http://localhost:1840/Tenants/ViewProfile/2

但是因为这个控制器操作是从登录的 Lanlord 调用的,所以 URL 看起来像这样:

http://localhost:1840/Landlords/Tenants/ViewProfile/2

我知道我需要创建一个新路由来忽略 URL 的“房东”部分,当房东从租户控制器调用操作时,我该怎么做?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing


    【解决方案1】:

    在租户前添加反斜杠

    var url = "/Tenants/ViewProfile";
    

    【讨论】:

    • 我实际上尝试了类似的东西,但没有在租户之前包含反斜杠。可以,谢谢。
    猜你喜欢
    • 2013-08-15
    • 1970-01-01
    • 2015-06-17
    • 1970-01-01
    • 2011-08-04
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多