컨트롤러에 동일한 이름을 가진 두 가지 동작이 있지만 일부 매개 변수를 허용하는 컨트롤러가 있습니다. 모호성을 제거하기 위해 GET 요청 만 허용하고 다른 요청은 POST 요청 만 허용합니다. 또한 액션 메소드에서 Ajax 호출 만 시행하는 데 사용되는 HttpAjaxAttribute가 있습니다. 어떤 이유로이 솔루션은 신뢰할 수 없습니다. 가끔 가져 오기 작업에 대한 GET 요청에서 MVC는 POST/AJAX를 고집하려고 시도하고 HttpAjaxAttribute에서 Ajax 예외를 throw합니다. 나는 a question that may be related을 발견했다. 특정 순서 (HttpGet 또는 HttpPost 및 HttpAjax)로 특성을 첨부하면 문제가 해결 될 것이라고 생각했지만 그렇지 않습니다. 내 웹 사이트가 얼마 동안 작동했지만 실패했습니다. 나는이 문제를 겉보기에 무작위로 발견했습니다. 어떻게 잘 해결할 수 있습니까?ASP.NET MVC가 잘못된 컨트롤러 동작을 선택했습니다.
컨트롤러 작업
[HttpGet]
public ActionResult Import()
{
// some code
}
[HttpPost]
[HttpAjax]
public ActionResult Import(string country, string state, string city, ImportModel[] locations)
{
// some code
}
HttpAjaxAttribute
/// <summary>
/// Makes the controller action that has this attribute applied accept only Ajax requests.
/// </summary>
public class HttpAjaxAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
if (!controllerContext.HttpContext.Request.IsAjaxRequest())
{
throw new Exception("The action " + methodInfo.Name + " can only be called via an Ajax request");
}
return true;
}
}
시작된다 요청을 로깅. – jgauffin
js 아약스 코드를 빠르게 살펴보면 도움이 될 것입니다 –