asp.net mvc路由问题重定向

时间:2021-07-19 04:13:30

I'm trying to create a route that redirects to urls but can't get it to work for the life of me. Here's what I have so far:

我正在尝试创建一个重定向到网址的路由,但无法让它在我的生活中工作。这是我到目前为止所拥有的:

public class GoAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Go";
        }
    }

    public override void RegisterArea( AreaRegistrationContext context )
    {
        context.MapRoute(
            "Go/Issues",
            "go/issues/{issueID}",
            new { controller = "Go", action = "GoIssues" },
            new { issueID = @"\d+" }
        );
    }
}

and my controller:

和我的控制器:

public class GoController : Controller
{
    public ActionResult GoIssues( int issueID )
    {
      var version = getVersion( issueID );
      if( version != null )
      {
        string url = MakeUrl(version, issueID);

        // Redirect to the right url
        return Redirect( url );
      }
    }

    // not found
    return HttpNotFound();
  }
}

The whole point of this is to determine the right version to redirect to and to redirect there. But for some reason go/issues/123 gives me a 404 not found. I used the route debug tool and it shows that this route is hit but when I disable it, I'm back to 404.

这一点的重点是确定重定向到的正确版本以及重定向到那里。但由于某种原因,go / issues / 123给了我404未找到。我使用了路由调试工具,它显示此路由被命中但是当我禁用它时,我又回到了404。

Any help is greatly appreciated.

任何帮助是极大的赞赏。

Thanks

谢谢

2 个解决方案

#1


1  

Try put your route in the global route configuration instead of area registration and see if works .

尝试将您的路线放在全局路线配置而不是区域注册,看看是否有效。

I've tried using your configuration on my test project, it's working fine. If this still doesn't work, I would suggest to start small and make it work, then slowly adding your route configuration onto it until a point it breaks.

我已经尝试在我的测试项目上使用你的配置,它工作正常。如果这仍然不起作用,我建议从小处开始使其工作,然后慢慢地将路径配置添加到它上面,直到它断开。

#2


0  

If anybody else is having the same problem, I've found the answer.

如果其他人遇到同样的问题,我找到了答案。

After much googling, I found the following article that explains how routing works.

经过大量的谷歌搜索后,我发现以下文章解释了路由的工作原理。

http://blog.davebouwman.com/2011/12/08/asp-net-mvc3-and-404s-for-area-controllers/

http://blog.davebouwman.com/2011/12/08/asp-net-mvc3-and-404s-for-area-controllers/

The article says that ASP.NET is expecting the namespace to be a certain format:

文章说ASP.NET期望命名空间是某种格式:

The issue was the namespace on my controller. Turns out that MVC has a convention that the area name is expected to be in the namespace of the controller.

问题是我的控制器上的命名空间。事实证明,MVC有一个约定,即区域名称应该在控制器的命名空间中。

So – I had StatsMe.API.CategoriesController, but MVC was looking for StatsMe.Areas.API.CategoriesController.

所以 - 我有StatsMe.API.CategoriesController,但MVC正在寻找StatsMe.Areas.API.CategoriesController。

What finally tipped me off was some stuff in the RouteDebugger output…

最终让我失望的是RouteDebugger输出中的一些东西......

Annoying because the error message really could have helped out by saying “Hey – I’m looking for StatsMe.Areas.API.CategoriesController and I can’t find it”, rather than the very generic message it spit out.

烦人,因为错误信息真的可以帮助说“嘿 - 我正在寻找StatsMe.Areas.API.CategoriesController我找不到它”,而不是它吐出的非常通用的消息。

#1


1  

Try put your route in the global route configuration instead of area registration and see if works .

尝试将您的路线放在全局路线配置而不是区域注册,看看是否有效。

I've tried using your configuration on my test project, it's working fine. If this still doesn't work, I would suggest to start small and make it work, then slowly adding your route configuration onto it until a point it breaks.

我已经尝试在我的测试项目上使用你的配置,它工作正常。如果这仍然不起作用,我建议从小处开始使其工作,然后慢慢地将路径配置添加到它上面,直到它断开。

#2


0  

If anybody else is having the same problem, I've found the answer.

如果其他人遇到同样的问题,我找到了答案。

After much googling, I found the following article that explains how routing works.

经过大量的谷歌搜索后,我发现以下文章解释了路由的工作原理。

http://blog.davebouwman.com/2011/12/08/asp-net-mvc3-and-404s-for-area-controllers/

http://blog.davebouwman.com/2011/12/08/asp-net-mvc3-and-404s-for-area-controllers/

The article says that ASP.NET is expecting the namespace to be a certain format:

文章说ASP.NET期望命名空间是某种格式:

The issue was the namespace on my controller. Turns out that MVC has a convention that the area name is expected to be in the namespace of the controller.

问题是我的控制器上的命名空间。事实证明,MVC有一个约定,即区域名称应该在控制器的命名空间中。

So – I had StatsMe.API.CategoriesController, but MVC was looking for StatsMe.Areas.API.CategoriesController.

所以 - 我有StatsMe.API.CategoriesController,但MVC正在寻找StatsMe.Areas.API.CategoriesController。

What finally tipped me off was some stuff in the RouteDebugger output…

最终让我失望的是RouteDebugger输出中的一些东西......

Annoying because the error message really could have helped out by saying “Hey – I’m looking for StatsMe.Areas.API.CategoriesController and I can’t find it”, rather than the very generic message it spit out.

烦人,因为错误信息真的可以帮助说“嘿 - 我正在寻找StatsMe.Areas.API.CategoriesController我找不到它”,而不是它吐出的非常通用的消息。