6
votes

I'm newbie to MVC. I could integrate MVC 5.2 to my existing web forms Visual Studio 2012 Update 4 project. I created my first controller and all worked as expected. Even I was able to leverage the windows forms authentication from my existing project when accessing the MVC view. But when created my second controller it began messing up.

It is my route mapping:

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.EnableFriendlyUrls();

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

I have two controllers both located in ~/Controllers. My first controller is:

public class HomeController : Controller
{
    //
    // GET: /Home/
    public ActionResult Index()
    {
        //return View();
        return Redirect("~/Default.aspx");
    }

    public ActionResult CloseSession()
    {
        return Redirect("http://www.yahoo.com");
    }
}

The second controller:

public class CajaWebController : Controller
{
    //
    // GET: /CajaWeb/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult CloseSession()
    {
        return Redirect("http://www.cnn.com");
    }
}

I don't know is it relevant to the problem but I'll include how the MVC view is reached. My VS2012 start url is

http://localhost/Fortia/CajaWeb. 

Fortia is my app name. Because I declared Web Forms authentication and

<location path="CajaWeb">
  <system.web>
    <authorization>
      <allow roles="Fortia" />
      <deny users="*" />
    </authorization>
  </system.web>
</location>

when starting to debug the old WebForms app authentication mechanism is called, the old WebForms login page invoked and after a successful login finally my CajaWebController, Index() action is called. Before creating CajaWebController it was the HomeController who was called, but I assume MVC now deduces the correct controller is CajaWeb because of the targeted url being

http://localhost/Fortia/CajaWeb. 

The invoked view contains the following code:

<a href='@Url.Action("CloseSession", "CajaWeb")'>Close session</a>

The problem is when clicking the generated link the MVC calls HomeController.Index() action despite I explicitly set CajaWebController.CloseSession() in the @Url.Action...

I looked at the generated link and it looks wrong:

<a href='/Fortia/__FriendlyUrls_SwitchView?action=CloseSession&amp;controller=CajaWeb'>

it encoded the parameter separator & into & But anyway I tried handcoding the href as

http://localhost/Fortia/__FriendlyUrls_SwitchView?action=CloseSession&controller=CajaWeb 

but the result was the same.

What is wrong?

2
Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?. - John Saunders
Perhaps the package ASP.NET Friendly URLs you seem to use for WebForms doesn't play nicely with MVC? - CodeCaster
NEW EVIDENCE! This code works: @Html.RouteLink("Link text to display on page", "Default", new System.Web.Routing.RouteValueDictionary(new { controller = "CajaWeb", action = "CloseSession", foo = "A", bar = "B" })); Then why the others not? - sammybar
May be @CodeCaster is in the correct trail. @Html.RouteLink generated <a href="/Fortia/CajaWeb/CloseSession?foo=A&amp;bar=B">, no "friendly urls"... - sammybar

2 Answers

0
votes

I think the problem is that the route

http://localhost/Fortia/CajaWeb

doesn't match any routes so it ends up going to the default route specified in RouteConfig. You need to configure a route or create an area in your app called "Fortia".

0
votes

It would seem the ASP.NET Friendly Urls package you're using is interfering with the urls MVC is generating. The library seems to be meant for WebForms anyway

If it works without, then leave it like that as MVC's urls are already quite SEO-friendly when controller and action names are meaningful to their content.