ASP.Net MVC 不断为我调用的每个控制器操作方法调用索引操作方法
ASP.Net MVC keeps calling the index action method for each controller action method I call
我的路由配置文件或 Asp.Net MVC 应用程序中的其他内容有问题,它不断触发我访问的每个控制器中的每个索引操作方法。如果我单击下面的操作 link,我将被带到页面 /profile/general,查看屏幕上呈现的视图结果,但应用程序中的某些东西(不确定是什么)会触发索引操作调用那个控制器!因为我在大多数控制器上没有索引操作方法,所以我最终在访问每个页面时遇到异常。
例如。我点击下面的动作link
@Html.ActionLink("Profile", "general", "profile", new { }, new { @class = "btn-sm", @style = "font-weight: bold; font-size: 1em;" })
或者只是刷新浏览器。
我进入下面看到的一般动作方法
public ActionResult General()
{
try
{
ViewBag.MenuItem = "profile";
ViewBag.UserMenuItem = "general";
var viewModel = _yogaProfileService.GetGeneralInfo(User.Identity.GetUserId());
return View(viewModel);
}
catch (Exception ex)
{
_errorService.LogError(ex, Request);
ViewBag.Message = "Oh No! Something went wrong fetching your info. We're looking into this now!";
return View("Error");
}
}
视图被调用,我在页面上看到它,但随后某些东西调用了同一控制器上的索引方法,我在这里进入索引操作方法。调试器进入下面的方法。它不会在屏幕上呈现索引的视图,我仍然看到一般视图,所以不确定这里发生了什么!
public ActionResult Index()
{
try
{
ViewBag.MenuItem = "profile";
return View();
}
catch (Exception ex)
{
_errorService.LogError(ex, Request);
ViewBag.Message = "Oh No! Something went wrong fetching your profile. We're looking into this now!";
return View("Error");
}
}
当然,出于显示的原因,我在这里只有索引方法,所以在应用程序中我将其删除,所以我几乎在访问的每个页面上都会抛出异常,因为它会在每个控制器上查找索引操作方法我访问
System.Web.HttpException: A public action method 'Index' was not found on controller 'YogaBandy2017.Controllers.ProfileController'.
不知道是路由问题还是别的什么?我尝试查看调试器以查看网络以确定调用索引的内容,以及调用堆栈中的内容,但它只是说 'external' 是否有可能某些 Javascript 正在调用它,如果是这样,如何调用我要找这个吗?
这是我的路由文件
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "RateRoute",
url: "rate/event/{id}",
defaults: new { controller = "rate", action = "event" },
constraints: new { id = @"\d+" }
);
routes.MapRoute(
name: "SpaceCleanRoute",
url: "space/{id}",
defaults: new { controller = "space", action = "index", id = UrlParameter.Optional },
constraints: new { id = @"\d+" }
);
routes.MapRoute(
name: "PublicSpaceRoute",
url: "space/public/{title}",
defaults: new { controller = "space", action = "public" },
constraints: new { title = @"^[A-Za-z0-9-]+$" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
更新 - 我只在 IE 中看到它,Chrome 和 Firefox 没有我的问题
这是我的调试器的图片,显示了待处理的请求
UPDATE2 - 我认为这不是 Javascript 问题,如果我不加载 .js 文件,索引仍会被调用。所以我不认为它与 Javascript
有任何关系
在 Whosebug 聊天中讨论后,我们发现 IE 11 正在触发对以下默认操作的调用:
<link href="" rel="icon">
IE 正在为控制器的默认操作生成 XMLHttpRequest。
这发生在 IE 11 中。
通过删除空 href=""
,我们能够解决问题。
解决方法如下:
<link rel="icon">
根据 HTML5 预览规范:
https://dev.w3.org/html5/spec-preview/the-link-element.html#attr-link-href
The destination of the link(s) is given by the href attribute, which
must be present and must contain a valid non-empty URL potentially
surrounded by spaces. If the href attribute is absent, then the
element does not define a link.
The types of link indicated (the relationships) are given by the value of the rel attribute, which, if present, must have a value that is a set of space-separated tokens. The allowed keywords and their meanings are defined in a later section. If the rel attribute is absent, has no keywords, or if none of the keywords used are allowed according to the definitions in this specification, then the element does not create any links.
Two categories of links can be created using the link element: Links
to external resources and hyperlinks. The link types section defines
whether a particular link type is an external resource or a hyperlink.
One link element can create multiple links (of which some might be
external resource links and some might be hyperlinks); exactly which
and how many links are created depends on the keywords given in the
rel attribute. User agents must process the links on a per-link basis,
not a per-element basis
The exact behavior for links to external resources depends on the
exact relationship, as defined for the relevant link type. Some of the
attributes control whether or not the external resource is to be
applied (as defined below).
For external resources that are represented in the DOM (for example,
style sheets), the DOM representation must be made available even if
the resource is not applied. To obtain the resource, the user agent
must run the following steps:
- If the href attribute's value is the empty string, then abort these steps.
- Resolve the URL given by the href attribute, relative to the
element.
- If the previous step fails, then abort these steps.
- Fetch the resulting absolute URL.
话虽如此,IE11 似乎并未因 "href" 值为空而中止上述定义的步骤...它继续解析步骤 4 中定义的外部资源。
Edge 对此有一些讨论。
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8088887/
我的路由配置文件或 Asp.Net MVC 应用程序中的其他内容有问题,它不断触发我访问的每个控制器中的每个索引操作方法。如果我单击下面的操作 link,我将被带到页面 /profile/general,查看屏幕上呈现的视图结果,但应用程序中的某些东西(不确定是什么)会触发索引操作调用那个控制器!因为我在大多数控制器上没有索引操作方法,所以我最终在访问每个页面时遇到异常。
例如。我点击下面的动作link
@Html.ActionLink("Profile", "general", "profile", new { }, new { @class = "btn-sm", @style = "font-weight: bold; font-size: 1em;" })
或者只是刷新浏览器。 我进入下面看到的一般动作方法
public ActionResult General()
{
try
{
ViewBag.MenuItem = "profile";
ViewBag.UserMenuItem = "general";
var viewModel = _yogaProfileService.GetGeneralInfo(User.Identity.GetUserId());
return View(viewModel);
}
catch (Exception ex)
{
_errorService.LogError(ex, Request);
ViewBag.Message = "Oh No! Something went wrong fetching your info. We're looking into this now!";
return View("Error");
}
}
视图被调用,我在页面上看到它,但随后某些东西调用了同一控制器上的索引方法,我在这里进入索引操作方法。调试器进入下面的方法。它不会在屏幕上呈现索引的视图,我仍然看到一般视图,所以不确定这里发生了什么!
public ActionResult Index()
{
try
{
ViewBag.MenuItem = "profile";
return View();
}
catch (Exception ex)
{
_errorService.LogError(ex, Request);
ViewBag.Message = "Oh No! Something went wrong fetching your profile. We're looking into this now!";
return View("Error");
}
}
当然,出于显示的原因,我在这里只有索引方法,所以在应用程序中我将其删除,所以我几乎在访问的每个页面上都会抛出异常,因为它会在每个控制器上查找索引操作方法我访问
System.Web.HttpException: A public action method 'Index' was not found on controller 'YogaBandy2017.Controllers.ProfileController'.
不知道是路由问题还是别的什么?我尝试查看调试器以查看网络以确定调用索引的内容,以及调用堆栈中的内容,但它只是说 'external' 是否有可能某些 Javascript 正在调用它,如果是这样,如何调用我要找这个吗?
这是我的路由文件
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "RateRoute",
url: "rate/event/{id}",
defaults: new { controller = "rate", action = "event" },
constraints: new { id = @"\d+" }
);
routes.MapRoute(
name: "SpaceCleanRoute",
url: "space/{id}",
defaults: new { controller = "space", action = "index", id = UrlParameter.Optional },
constraints: new { id = @"\d+" }
);
routes.MapRoute(
name: "PublicSpaceRoute",
url: "space/public/{title}",
defaults: new { controller = "space", action = "public" },
constraints: new { title = @"^[A-Za-z0-9-]+$" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
更新 - 我只在 IE 中看到它,Chrome 和 Firefox 没有我的问题 这是我的调试器的图片,显示了待处理的请求
UPDATE2 - 我认为这不是 Javascript 问题,如果我不加载 .js 文件,索引仍会被调用。所以我不认为它与 Javascript
有任何关系在 Whosebug 聊天中讨论后,我们发现 IE 11 正在触发对以下默认操作的调用:
<link href="" rel="icon">
IE 正在为控制器的默认操作生成 XMLHttpRequest。
这发生在 IE 11 中。
通过删除空 href=""
,我们能够解决问题。
解决方法如下:
<link rel="icon">
根据 HTML5 预览规范:
https://dev.w3.org/html5/spec-preview/the-link-element.html#attr-link-href
The destination of the link(s) is given by the href attribute, which must be present and must contain a valid non-empty URL potentially surrounded by spaces. If the href attribute is absent, then the element does not define a link.
The types of link indicated (the relationships) are given by the value of the rel attribute, which, if present, must have a value that is a set of space-separated tokens. The allowed keywords and their meanings are defined in a later section. If the rel attribute is absent, has no keywords, or if none of the keywords used are allowed according to the definitions in this specification, then the element does not create any links.
Two categories of links can be created using the link element: Links to external resources and hyperlinks. The link types section defines whether a particular link type is an external resource or a hyperlink. One link element can create multiple links (of which some might be external resource links and some might be hyperlinks); exactly which and how many links are created depends on the keywords given in the rel attribute. User agents must process the links on a per-link basis, not a per-element basis
The exact behavior for links to external resources depends on the exact relationship, as defined for the relevant link type. Some of the attributes control whether or not the external resource is to be applied (as defined below).
For external resources that are represented in the DOM (for example, style sheets), the DOM representation must be made available even if the resource is not applied. To obtain the resource, the user agent must run the following steps:
- If the href attribute's value is the empty string, then abort these steps.
- Resolve the URL given by the href attribute, relative to the element.
- If the previous step fails, then abort these steps.
- Fetch the resulting absolute URL.
话虽如此,IE11 似乎并未因 "href" 值为空而中止上述定义的步骤...它继续解析步骤 4 中定义的外部资源。
Edge 对此有一些讨论。
https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/8088887/