ASP.net MVC 5路由:加载基本url时出现ajax错误,但在末尾添加主页/索引时有效



如果没有/Home,我的项目的基本url将无法工作。最后,它返回一个ajax错误。如果输入的基本url没有Home/Index,我如何确保它在不返回错误的情况下工作?

我可以看到,在服务器端GetTaskLog返回了一个404未找到的错误。GetTaskLog是我的家庭控制器中的一个函数,用于从数据库中检索数据。当我将/home添加到url时,GetTaskLog运行良好。它检索数据并且没有错误。

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

Ajax:

"ajax": {
"url": "../Home/GetTaskLog",
"type": 'POST',
"content-type": "application/json; charset=utf-8",
"processData": false,
"dataType": "json",
"headers": "headers",
"data": "window.JSON.stringify(obj)",

$('input[name=__RequestVerificationToken]').val()},"传统的":是的,"dataSrc":函数(数据){返回JSON.parse(数据);},},

路由中的默认值。MapRoute表示,如果应用程序的url上没有类似于{controller}/{action}/{id}的内容,它将返回Home/Index。因此,您需要更改默认参数

我的问题的解决方案是将ajax url更改为:

"ajax": {
"url": "/WTM/Home/GetTaskLog"
}

最新更新