How do you get the ASP.NET MVC action of the page (i.e. one from URL) rather than current action?

Mark Erasmus

Given the following simplified ASP.NET MVC scenario:

  1. A user navigates to a page http://www.mysite.com/Home/Index ("Index" explictly included for clarity)
  2. On that page is a $.ajax({..}) jQuery post that calls a method within Home controller, e.g. /Home/GetProducts
  3. In the 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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Using nameof() with Url.Action() and async methods in ASP.NET Core 3.x MVC

분류에서Dev

ASP.NET MVC 5의 Url.Action에 동적 값 전달

분류에서Dev

DB Context is created every time I call an Action in ASP.NET MVC

분류에서Dev

How do I get this CSS/jQuery menu to open only on click, rather than hover?

분류에서Dev

How do I get Dropdown SelectedItem text in view model in ASP.NET MVC 3

분류에서Dev

How do I prevent ASP.NET MVC from binding query parameters?

분류에서Dev

Tow way for call a action in asp.net mvc with route attribute - Routing In asp.net mvc

분류에서Dev

How do you bind-attr and action on the same element in handlebars?

분류에서Dev

How can i get all selected options from a select (multiple) in as ASP.NET MVC with Request.Form?

분류에서Dev

how to Change the url of page asp.net?

분류에서Dev

ASP.NET Core 3.x MVC에서 Url.Action () 및 비동기 메서드와 함께 nameof () 사용

분류에서Dev

ASP.NET MVC 라우팅-경로 값이 아닌 쿼리 문자열을 보내는 Url.Action

분류에서Dev

Is it possible to avoid the form action url from getting appended to the url in the address bar.? Or how can i remove finally?

분류에서Dev

How do i get the page numbers from the example (with PHP)

분류에서Dev

How do you start more than one thread in C++

분류에서Dev

ASP.NET MVC : Html.Action / Html.ActionLink 및 문화권 별 DateTime

분류에서Dev

Construct and pass JavaScript string list to ASP.NET MVC Controller action (not using ajax)

분류에서Dev

Mvc의 ASP.NET 라우팅 통합 : Controller / Action-to-Route-Resolution

분류에서Dev

ASP.NET MVC의 ActionName 특성 및 UrlHelper.Action 메서드

분류에서Dev

Selenium IDE - how to get result of verifyNotText and do the right action

분류에서Dev

How to get Word to do line break on Enter rather than paragraph break?

분류에서Dev

How to get the url with application name in .net mvc?

분류에서Dev

How do I achive this exact behavior using divs, rather than using table, tr, and td?

분류에서Dev

How do I display calculated values in plain numbers rather than powers in Android.?

분류에서Dev

How can I get PyCharm to use the Vagrant Directories rather than my development machines?

분류에서Dev

How do I handle a related models add action in cakephp?

분류에서Dev

How do I get the image url from an image field type

분류에서Dev

How do I create pretty url from the GET parameters in Django?

분류에서Dev

How to Get Method Name From Action in Windows Store Application

Related 관련 기사

  1. 1

    Using nameof() with Url.Action() and async methods in ASP.NET Core 3.x MVC

  2. 2

    ASP.NET MVC 5의 Url.Action에 동적 값 전달

  3. 3

    DB Context is created every time I call an Action in ASP.NET MVC

  4. 4

    How do I get this CSS/jQuery menu to open only on click, rather than hover?

  5. 5

    How do I get Dropdown SelectedItem text in view model in ASP.NET MVC 3

  6. 6

    How do I prevent ASP.NET MVC from binding query parameters?

  7. 7

    Tow way for call a action in asp.net mvc with route attribute - Routing In asp.net mvc

  8. 8

    How do you bind-attr and action on the same element in handlebars?

  9. 9

    How can i get all selected options from a select (multiple) in as ASP.NET MVC with Request.Form?

  10. 10

    how to Change the url of page asp.net?

  11. 11

    ASP.NET Core 3.x MVC에서 Url.Action () 및 비동기 메서드와 함께 nameof () 사용

  12. 12

    ASP.NET MVC 라우팅-경로 값이 아닌 쿼리 문자열을 보내는 Url.Action

  13. 13

    Is it possible to avoid the form action url from getting appended to the url in the address bar.? Or how can i remove finally?

  14. 14

    How do i get the page numbers from the example (with PHP)

  15. 15

    How do you start more than one thread in C++

  16. 16

    ASP.NET MVC : Html.Action / Html.ActionLink 및 문화권 별 DateTime

  17. 17

    Construct and pass JavaScript string list to ASP.NET MVC Controller action (not using ajax)

  18. 18

    Mvc의 ASP.NET 라우팅 통합 : Controller / Action-to-Route-Resolution

  19. 19

    ASP.NET MVC의 ActionName 특성 및 UrlHelper.Action 메서드

  20. 20

    Selenium IDE - how to get result of verifyNotText and do the right action

  21. 21

    How to get Word to do line break on Enter rather than paragraph break?

  22. 22

    How to get the url with application name in .net mvc?

  23. 23

    How do I achive this exact behavior using divs, rather than using table, tr, and td?

  24. 24

    How do I display calculated values in plain numbers rather than powers in Android.?

  25. 25

    How can I get PyCharm to use the Vagrant Directories rather than my development machines?

  26. 26

    How do I handle a related models add action in cakephp?

  27. 27

    How do I get the image url from an image field type

  28. 28

    How do I create pretty url from the GET parameters in Django?

  29. 29

    How to Get Method Name From Action in Windows Store Application

뜨겁다태그

보관