ASP.NET MVC路由问题 [英] ASP.NET MVC Routing Question

查看:190
本文介绍了ASP.NET MVC路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一定要密实。问在计算器上几个问题后,我仍处于亏损状态,当谈到抓提供ASP.NET MVC新的路由引擎。我想我已经缩小的问题一个非常简单的,而如果解决了,可能会允许我解决我的路由问题休息。所以在这里,它是:

您将如何登记支持类似Twitter的网址为用户配置文件的路径?


  

www.twitter.com/username


假设需要还支持:


  • 默认 {控制器} / {行动} / {ID} 路线。


  • 网址,如:


      

    www.twitter.com/login

       www.twitter.com/register



这可能吗?


解决方案

什么

  routes.MapRoute(
    情景模式
    {用户名},
    新{控制器=情景模式,行动=ShowUser}
);

和然后,在ProfilesController,将有一个功能

 公众的ActionResult ShowUser(用户名字符串)
{
...

在功能,如果与指定的用户不被发现,你应该重定向到默认{控制器} / {行动} / {ID}(在这里,它也不过{控制器})路线。

的url像www.twitter.com/login应该是一前进行登记。

  routes.MapRoute(
    登录,
    登录,
    新{控制器=安全,行动=登录}
);

I must be dense. After asking several questions on StackOverflow, I am still at a loss when it comes to grasping the new routing engine provided with ASP.NET MVC. I think I've narrowed down the problem to a very simple one, which, if solved, would probably allow me to solve the rest of my routing issues. So here it is:

How would you register a route to support a Twitter-like URL for user profiles?

www.twitter.com/username

Assume the need to also support:

  • the default {controller}/{action}/{id} route.

  • URLs like:

    www.twitter.com/login
    www.twitter.com/register

Is this possible?

解决方案

What about

routes.MapRoute(
    "Profiles",
    "{userName}",
    new { controller = "Profiles", action = "ShowUser" }
);

and then, in ProfilesController, there would be a function

public ActionResult ShowUser(string userName)
{
...

In the function, if no user with the specified userName is found, you should redirect to the default {controller}/{action}/{id} (here, it would be just {controller}) route.

Urls like www.twitter.com/login should be registered before that one.

routes.MapRoute(
    "Login",
    "Login",
    new { controller = "Security", action = "Login" }
);

这篇关于ASP.NET MVC路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆