Given the following simplified ASP.NET MVC scenario:
$.ajax({..}
) jQuery post that calls a method within Home controller, e.g. /Home/GetProducts
GetProducts()
method I need to get the Index action name - bear in mind that at runtime I don't know whether the user is browsing Home/Index
, Home/About
, Home/Contact
, etc., as GetProducts
could be called from anywhere.나는 평생 동안 GetProducts()
메서드 의 범위에서 페이지 작업 (예 : Index, About, Contact 등)을 얻을 수 없습니다 .
나는 다음을 시도했다 :
// returns "GetProducts"
string actionName1 = RouteData.GetRequiredString("action");
// returns "GetProducts"
string actionName2 = ControllerContext.Controller.ValueProvider.GetValue("action").RawValue.ToString();
// ParentActionViewContext == null
string actionName3 = ControllerContext.ParentActionViewContext.RouteData.Values["action"].ToString();
당신은 그것을 얻을 수 없습니다. HTTP는 이전 요청을 추적하지 않는 상태 비 저장 프로토콜입니다. 따라서 AJAX 요청에 매개 변수로 전달하면됩니다.
$.ajax({
url: '@Url.Action("GetProducts", "Home")',
data: { currentAction: '@ViewContext.RouteData.GetRequiredString("action")' },
success: function(result) {
// do something with the results
}
});
당신의 GetProducts
컨트롤러 액션은 매개 변수로 걸릴 것입니다 :
public ActionResult GetProducts(string currentAction)
{
...
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다