ASP.Net MVC @ Url.Action()路由到错误的控制器

时间:2021-07-11 09:02:29

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.

我是MVC的新手。我可以将MVC 5.2集成到我现有的Web表单Visual Studio 2012 Update 4项目中。我创建了我的第一个控制器,并且按预期工作。甚至在访问MVC视图时我也能够从现有项目中利用Windows窗体身份验证。但是当创建我的第二个控制器时,它开始变得混乱。

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:

我有两个控制器都位于〜/ Controllers中。我的第一个控制器是:

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

我不知道它与问题有关,但我将包括如何达到MVC视图。我的VS2012启动网址是

http://localhost/Fortia/CajaWeb. 

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

Fortia是我的应用名称。因为我声明了Web Forms身份验证和

<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

当开始调试时,调用旧的WebForms应用程序身份验证机制,调用旧的WebForms登录页面,并在成功登录后最终调用我的CajaWebController,Index()动作。在创建CajaWebController之前,它是被调用的HomeController,但我认为MVC现在推断出正确的控制器是CajaWeb,因为目标url是

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...

问题是当单击生成的链接MVC调用HomeController.Index()时,尽管我在@ Url.Action中明确设置了CajaWebController.CloseSession()...

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

它编码参数分隔符并进入&但无论如何我尝试将href手动编码为

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

but the result was the same.

但结果是一样的。

What is wrong?

哪里不对?

2 个解决方案

#1


0  

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

看起来你正在使用的ASP.NET Friendly Urls包正在干扰MVC正在生成的URL。无论如何,该库似乎都适用于WebForms

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.

如果它没有工作,那么就这样吧,因为当控制器和动作名称对他们的内容有意义时,MVC的网址已经非常适合搜索引擎优化。

#2


0  

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".

与任何路由都不匹配,因此它最终会转到RouteConfig中指定的默认路由。您需要在应用中配置路径或创建一个名为“Fortia”的区域。

#1


0  

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

看起来你正在使用的ASP.NET Friendly Urls包正在干扰MVC正在生成的URL。无论如何,该库似乎都适用于WebForms

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.

如果它没有工作,那么就这样吧,因为当控制器和动作名称对他们的内容有意义时,MVC的网址已经非常适合搜索引擎优化。

#2


0  

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".

与任何路由都不匹配,因此它最终会转到RouteConfig中指定的默认路由。您需要在应用中配置路径或创建一个名为“Fortia”的区域。