在服务器ajax上部署MVC应用时,无法找到操作,但Localhost可以 [英] When deploying MVC app on server ajax cant find action but Localhost it does
问题描述
我有一个MVC5应用程序.当我在本地主机上运行它时,一切正常,没有任何错误.
I have a MVC5 application. When I run it on my localhost everything works without any errors.
发布应用程序后,将其传输到Windows Server 2016,将文件放在IIS文件夹中的wwwroot中,并链接所有内容以在IIS中创建新网站.然后,我运行该网站,它可以正常工作.我可以使用JavaScript代码工作,但是当我运行"ajax"方法时,出现404错误,并且在函数中找不到控制器操作,因此该方法可以工作.
When I publish my app then I transfer it to Windows Server 2016, I put the files in wwwroot in the IIS folder and I link everything to create a new website in IIS. I then run the website and it works. I get my javascript code to work, but when I go and run my ‘ajax’ methods I get an 404 error and in the function I cannot find my controller action so the method will work.
这是我的实际错误:
xxx.xxx.xx x.219/Parts/DoPartBookFunc?bookval = 8404(未找到), 加载资源失败:服务器响应状态为404 (未找到)
xxx.xxx.xx x.219/Parts/DoPartBookFunc?bookval=8 404 (Not Found), Failed to load resource: the server responded with a status of 404 (Not Found)
我一直在研究并尝试各种不同的方法,但是到目前为止还没有运气.我尝试过的一些事情是
I been researching and trying a bunch of different things, but so far no luck. Some things I tried were
-
@Url.action(,)
- 在前面添加〜
- 在前面添加../
- 制作全局文件
@Url.action("","")
- adding a ~ in front
- adding ../ in front
- making a global file
和许多其他事情.如果有人知道如何解决此问题,将不胜感激.
and many other things. If someone knows how to fix this it would be hugely appreciated.
$("#PartBook").on("change", function () {
var selectV = $(this).val();
var selectT = $(this).text();
$.ajax({
url: '/Parts/DoPartBookFunc',
type: 'GET',
dataType: 'json',
data: { bookval: selectV },
//contentType: 'application/json; charset=utf-8',
success: function (data) {
//alert("s" + data.PartNextNumber);
这是我所有人的解决方案!
Here is my Solution everybody!
将我的ajax()网址更改为:
Changed my ajax() url to:
url: "@Url.Action("DoPartBookFunc", "Parts")",
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
/* Newly Added */
routes.MapRoute(
name: "AddNewControllerName",
url: "AddNewControllerName/{action}/{id}",
defaults: new { controller = "AddNewControllerName", action = "Index", id = UrlParameter.Optional }
);
/* Old Route */
routes.MapRoute(
name: "mass",
url: "{action}/{id}",
defaults: new { controller = "Parts", action = "Index", id = UrlParameter.Optional }
);
推荐答案
尝试从网址中删除/:
$("#PartBook").on("change", function () {
var selectV = $(this).val();
var selectT = $(this).text();
$.ajax({
url: 'Parts/DoPartBookFunc',
type: 'GET',
dataType: 'json',
data: { bookval: selectV },
//contentType: 'application/json; charset=utf-8',
success: function (data) {
//alert("s" + data.PartNextNumber);
...
})
或尝试添加此内容:
var RootUrl = '@Url.Content("~/")';
$.ajax({
type: "POST",
url: RootUrl + "Parts/DoPartBookFunc",
这篇关于在服务器ajax上部署MVC应用时,无法找到操作,但Localhost可以的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!