问题描述
如果从页面本地主机:NNNN / I级点击一个AJAX链接,将张贴到'级/ AddClass我得到的类/ AddClass的RawUrl和它的作品就好了
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.
如果从页面本地主机:NNNN /班/索引我点击我得到的类/类/ AddClass和一个RawUrl它(显然)不工作相同的链接
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.
我意识到我在路由地狱,但谁是重写URL,为什么?我费尽周折通过jQuery的code加大,实际上它发布到类/ AddClass。
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
推荐答案
这不是重写这就是问题所在。您的AJAX请求是JavaScript无关,与ASP.NET路由引擎。当您使用类/ AddClass
你是使其相对于当前URL的位置。您可以使用 /班/ AddClass
这将解决该站点的根目录。这提出了一个问题,如果你是,即使在一个虚拟目录。我preFER拉从一个配置文件的完整URL:
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';
通过在web.config中的相应条目。这消除了任何猜测工作。您也可以使用 RESOLVEURL
:
With the appropriate entry in the web.config. This eliminates any guess work. You can also use ResolveUrl
:
var url = '<%: ResolveUrl("/Class/AddClass") %>';
这篇关于在AJAX后MVC 3重复的控制器名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!