MVC 3 Duplicated Controller names in AJAX post

自闭症网瘾萝莉.ら 提交于 2019-12-24 15:15:59

问题


If from the page localhost:nnnn/Class I click on an AJAX link that will post to 'Class/AddClass' I get a RawUrl of Class/AddClass and it works just fine.

If from the page localhost:nnnn/Class/Index I click the same link I get a RawUrl of Class/Class/AddClass and it (obviously) doesn't work.

I realize I'm in Routing Hell, but who's rewriting the URL and why? I painstakingly stepped through the jQuery code and indeed it's posting to Class/AddClass.

Thanks for insight... Eric


回答1:


It is not rewriting that is the problem. Your AJAX request is JavaScript and has nothing to do with the ASP.NET routing engine. When you use Class/AddClass you are making it relative to the location of the current URL. You can use /Class/AddClass which will resolve to the root of the site. That poses an issue if you are even in a virtual directory. I prefer to pull the full URL from a configuration file:

var url = '<%: ConfigurationManager.AppSettings["WebsiteURL"] %>/Class/AddClass';

With the appropriate entry in the web.config. This eliminates any guess work. You can also use ResolveUrl:

var url = '<%: ResolveUrl("/Class/AddClass") %>';


来源:https://stackoverflow.com/questions/6158256/mvc-3-duplicated-controller-names-in-ajax-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!