繁体   English   中英

如何为www.website.com/profile创建路由

[英]How to create a route for www.website.com/profile

我正在编写一个类似平台的网站,我们可以从以下URL访问用户个人资料:

www.mywebsite.com/DanielVC

具有有关配置文件详细信息的控制器如下

  • 控制器 :Perfil
  • 动作 :Perfil

我已经对所有应用程序具有以下路由:

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

我已经尝试创建以下路线:

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

但是没用

将其放在默认路由之前(上方)。

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

同样,这将导致每个路由都被重定向到Perfil路由。 如果找不到用户名(例如,mywebsite.com / randomuserthatdoesntexist)和/或其他路由(mywebsite.com/contact),则必须在该操作中创建重定向。

编辑

方法示例

public class PagesController : Controller
{
    public ActionResult Index(string id)
    {
        if (matchesOtherRoute(id))
            RedirectToAction("OtherAction", "OtherController");
        if (!userExists(id))
            RedirectToAction("NotFoundAction", "ErrorController");
        // Do other stuff here
     }
}

它应该类似于route.MapRoute(name:“ Perfil”,url:“ Pages / {id}”,默认值:new {controller =“ Perfil”,action =“ Perfil”,id = UrlParameter.Optional});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM