jugg1es
jugg1es

Reputation: 1600

MVC Form Action URL being set incorrectly

I'm developing my first MVC app and I'm already having an issue with BeginForm. When I test the project locally, the login form action is being set (correctly) to

<form action="/Account/Login"

When I publish it to the actual site, it is inexplicably prepending the application name to the action so it looks like this

<form action="/appName/Account/Login"

so when you try to actually login, the routing tables don't know where to post the request and nothing happens when you press Submit. Is there a way I can override this behavior?

Login.cshtml

    @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
 {
............ form
  }

RoutConfig.cs

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

Upvotes: 0

Views: 677

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

There is nothing wrong with your code. If you hosted your application as a virtual directory or application under a website in IIS it is perfectly normal that the ASP.NET MVC helpers such as Html.BeginForm are taking this application name into account. So the /appname prefix you are seeing represent the application name in IIS under the website. You can have multiple ASP.NET MVC applications hosted under a single website and as you can see the ASP.NET MVC helpers are correctly taking them into account.

If on the other hand you host your application in IIS directly as a WebSite, without creating any sub-applications, the url helpers will take that into account and generate action="/Account/Login".

Upvotes: 1

Related Questions