简体   繁体   English

如何解决MVC属性路由中的斜杠“ /”问题?

[英]How to fix end slash “/” problem in MVC Attribute routing?

I have a fixed link for my project like 我的项目有固定链接,例如

Link: domain/controller/firstname-lastname/ 链接:域/控制器/名字-姓氏/

Where Url_name ='firstname-lastname' is a parameter, that's why I had to use attribute routing to hide the action from the URL. 其中Url_name ='firstname-lastname'是一个参数,这就是为什么我不得不使用属性路由来隐藏URL中的操作的原因。

The list is coming from page 1 and there is a list of links for different user Like 该列表来自第1页,并且有一个不同用户的链接列表,如:

<a class="artist" id=' + data.Id + ' href="Contr/' + data.Url_name + '">' NAME </a>

1.domain/controller/firstname1-lastname/ 1.domain / controller / firstname1-lastname /

2.domain/controller/firstname2-lastname/ 2.domain / controller / firstname2-lastname /

..... .....

Then when the user clicks on the link system will go to the second page (page 2) with the Url_name only. 然后,当用户单击链接系统时,将仅使用Url_name转到第二页(第2页)。

[Route("Contr/{name}")]
public ActionResult Index(string name)
{
    return View();
}

At this point when the view will load them in the Javascript, I tried to take the name from the URL like 此时,当视图将它们加载到Javascript中时,我试图从URL中获取名称,例如

var url = document.URL;
var lastChar = url[url.length - 1];
var name;
var splittedStr = url.split('/');
if (lastChar !== "/") 
{       
    name = splittedStr[splittedStr.length - 1];
} 
else 
{       
    name = splittedStr[splittedStr.length - 2];
}

Then I am calling another action through ajax(Script side) to get the data to display. 然后,我通过ajax(脚本端)调用另一个动作以获取要显示的数据。

var url = "GetData/" + name + "/" + artistId + "/" + direction + "";
$.ajax({
        url: url,
        type: "GET",
        dataType: 'json',

        // data: { artistId: artistId, 'direction': 'none' },
        contentType: 'application/json; charset=utf-8',

        success: function (data) {
            $("#hidId").val(data.Id);
            $("#aName").html(data.Vorname + " " + data.Name);
            //and so on.
        }
});

The corresponding Action in the controller is 控制器中的相应动作为

[Route("Contr/GetData/{name?}/{aId:int?}/{direction?}")]       
public ActionResult GetData(string name,int? aId, string direction)
{ 
    //searcing code for coming paraeter
}

My problem is, in the link if there is no slash ("/") at the tail then everything is working correctly. 我的问题是,如果链接的末尾没有斜杠(“ /”),则说明一切正常。 But if there is then the call is concatenating with the present url like 但是,如果存在,则该调用将与当前url并置,例如

domain/controller/firstname1-lastname/GetData/name/0/none 域/控制器/名字1-姓氏/ GetData /名字/ 0 /无

And it's not calling the GetData action in the controller. 而且它没有在控制器中调用GetData操作。 Could anyone help in this situation ?? 在这种情况下,有人可以帮忙吗?

I don't know if its the right way or not but I have solved the problem by resetting the URL without a slash("/")> 我不知道它是否正确,但是我已经通过不带斜杠(“ /”)>的URL重置来解决了问题

window.location = url.substr(0, url.length-1);

as far as I learn if there is a slash("/") then it waiting for the next string to append for the attribute routing. 据我了解,如果有一个斜杠(“ /”),那么它等待下一个字符串追加到属性路由。 that was the problem!!! 那就是问题所在!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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