Domanda

I have an existing ASP.NET MVC 4 application that makes use of the HtmlHelper.BeginForm methods within the views. This site is now going to sit behind a reverse proxy which will add prepend some characters into the path part of the URL. I was hoping to create an extension method to HtmlHelper that would allow me to do the following:

@using (Html.BeginForm("ActionName", "MyControllerName", FormMethod.Post, new { @class = "my-class" })){

And be able to modify the output from:

<form method="POST" action="/MyController/ActionName" class="my-class">

to the following instead:

<form method="POST" action="/Some/Prepended/Path/MyController/ActionName" class="my-class">
È stato utile?

Soluzione

You can use FormExtensions.BeginRouteForm method to achieve this.

First, create a route in RouteConfig class like below:

    routes.MapRoute(
    name: "FormRoute",
    url: "Some/Prepended/Path/{controller}/{action}"
    );

then use Html.BeginRouteForm

@using (Html.BeginRouteForm("FormRoute", new { }, 
         FormMethod.Post, new { @class = "my-class" })){}

Finally, you can call your action method like below:

http://localhost:57744/Some/Prepended/Path/YourController/YourAction

For more details about FormExtensions.BeginRouteForm Method, see below link: http://msdn.microsoft.com/en-us/library/dd505047(v=vs.118).aspx

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top