简体   繁体   中英

Removing a controller name from a URL in MVC4

I'm dynamically constructing URLs to pass to a jQuery button click event handler.

The URL comes from a modal window definition:

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

The id comes from a button:

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

In my JS function I grab both pieces of data

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

and construct the URL like this $.get(url + '/' + id, function (data))...

To work, the URL should look like this:

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

but because this controller action is invoked from a logged in Lanlord the URL looks like this:

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

I understand that I need to create a new route to ignore the 'Landlord' part of the URL, how do I do this when a landlord is invoking an action from the tenant contoller?

在租户之前添加反斜杠

var url = "/Tenants/ViewProfile";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM