iJade
iJade

Reputation: 23791

Why Not looking for the View in the correct location

I'm an MVC newbie so this might sound trivial.I have my 2 Views(EnterLogin.aspx,ShowLogin.aspx) in a folder called LoginForm in Views. Here is my Global.asax.cs below

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "LoginForm", action = "ShowLogin", id = UrlParameter.Optional } // Parameter defaults
        );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }
}

Here is my ShowLogin.aspx design code

<form method="post" action="EnterLogin" runat="server">
    Hello, i'm login page
    Enter Name   <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    <input type="submit" value="PressMe" />
</form>

Here are my controllers

public class LoginFormController : Controller
{
    public ActionResult ShowLogin()
    {
        return View();
    }

    public ActionResult EnterLogin()
    {
        return View("EnterLogin");
    }
}

On running the application it first loads with url

http://localhost:50224/

and shows the ShowLogin.aspx View

On clicking the button I'm calling EnterLogin controller to show EnterLogin View but it looks in URL

http://localhost:50224/EnterLogin

instead of

http://localhost:50224/LoginForm/EnterLogin

What could be causing this?

Upvotes: 1

Views: 101

Answers (3)

Abbas Galiyakotwala
Abbas Galiyakotwala

Reputation: 3019

You may do something like this

Global.asax.cs :-

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "LoginForm", action = "ShowLogin", id = UrlParameter.Optional } // Parameter defaults
    );

ShowLogin.aspx :-

 <form method="post" action="EnterLogin" runat="server">
     Hello, i'm login page
    Enter Name   <input type="text" name="txtName"/>
     <input type="submit" value="PressMe" /> </form>

LoginFormController :-

public class LoginFormController : Controller
{
    public ActionResult ShowLogin()
    {
        return View();
    }

    [HttpPost]
    public ActionResult EnterLogin(FormCollection collection)
    {
        string Yourtxtname=Collection["txtName"]; //You will get input text value

        return View();
    }
}

Upvotes: 0

Brad Christie
Brad Christie

Reputation: 101594

The reason you're not landing where you want to is the action portion of your form:

action="EnterLogin"

This should follow the correct route to ensure it hits the LoginFormController. e.g.

action="/LoginForm/EnterLogin"

Remember that the incoming request needs to match a route specified in RegisterRoutes. Because you don't have anything directly matching EnterLogin it will try to use EnterLogin to fill in the {controller} then default to ShowLogin as the action (resulting in a failed request). basically:

EnterLogin  ==resolves==>  EnterLogin  /ShowLogin/
                           {controller}/{action} /{id}

Alternatively you can make a named route that will redirect to the correct location if you want to short-hand it:

action="LogMeIn"

and then:

routes.MapRoute(
    "Login",
    "LogMeIn",
    new { controller = "LoginForm", action = "EnterLogin" }
);

Now requesting /LogMeIn will execute LoginForm's EnterLogin() action.

Upvotes: 1

John Mc
John Mc

Reputation: 2932

The default route defined in Global.asax defines routes which have /controller/action.

Your controller is called LoginForm and your action is called EnterLogin so this is expected behaviour.

If you want to exclude LoginForm from the URL, you need to define a custom route to allow for this.

 routes.MapRoute(
        "LoginForm_EnterLogin", // Route name
        "LoginForm/EnterLogin/{id}", // URL with parameters
        new { controller = "LoginForm", action = "ShowLogin", id = UrlParameter.Optional } // Parameter defaults
    );

Upvotes: 0

Related Questions