问题
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