Ajax doesnt work when I publish to my MVC3 project
I have a problem in my MVC3 project about using ajax. I have done couple of settings for using it like add some scripts in master page and change some stuff in web.config file etc.
Actually after all those things my project works fine in my local server. But when I publish it, ajax stuff doesnt work any more.
Do you have any idea for solving this problem? what do ı neen to do? It would be great if someone could help me.
Thanks in advance.
These things are in my master page.
<script src="/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
And these are from web.config I have just changed UnobtrusiveJavaScriptEnabled value from "true" to "false"
<configuration>
<appSettings>
<add key="inspector" value="EmrTelInspector" />
<add key="browserExpireCookie" value="EmrTel" />
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="false" /&g开发者_如何学Got;
</appSettings>
Replace:
<script src="/Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script>
<script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
with:
<script src="@Url.Content("~/Scripts/MicrosoftAjax.debug.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
or if you are using the WebForms view engine:
<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.debug.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js") %>" type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js") %>" type="text/javascript"></script>
The reason why your code doesn't work when you publish is because there is a virtual directory in IIS so the correct url is no longer /Scripts/MicrosoftAjax.debug.js
but /NameOfYourApplication/Scripts/MicrosoftAjax.debug.js
.
For this reason you should never hardcode any urls in an ASp.NET MVC application. You should always use url helpers to ensure that this application will work no matter where it is deployed.
精彩评论