Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编写包含用户名的路由_C#_Asp.net Mvc_Model View Controller - Fatal编程技术网

C# 如何编写包含用户名的路由

C# 如何编写包含用户名的路由,c#,asp.net-mvc,model-view-controller,C#,Asp.net Mvc,Model View Controller,我正在建立一个asp.net mvc网站,在用户登录后,他可以访问他的个人资料部分页面,目前这些页面的URL类似于www.example.com/profile,我想要的是使URL类似于www.example.com/USERNAME 当用户登录时,如何编写仅在配置文件页面工作的路由 更新: 根据下面的答案,我这样写: routes.MapRoute( "AccountSettins", "AccountSettings",

我正在建立一个asp.net mvc网站,在用户登录后,他可以访问他的个人资料部分页面,目前这些页面的URL类似于www.example.com/profile,我想要的是使URL类似于www.example.com/USERNAME

当用户登录时,如何编写仅在配置文件页面工作的路由

更新:

根据下面的答案,我这样写:

routes.MapRoute(
                "AccountSettins",
                   "AccountSettings",
               new { controller = "AccountSettings", action = "Index" }
            );

            routes.MapRoute(
                 "myRouteName",
                    "{username}",
                new { controller = "Profile", action = "Index" }
             );


            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
以及控制器:

[Authorize]
    public class ProfileController : BaseController
    {
        //
        // GET: /Profile/

        public ActionResult Index(string username= "")
        { ...
但是现在,在用户登录并且用户名为“xyz”之后,他可以访问www.example.com/xyz,这将导致配置文件页面,但是如果他也编写了url www.example.com/abc,他通常会访问同一个配置文件页面,从用户的角度来看,这很奇怪,如何解决这个问题

routes.MapRoute(
    "myRouteName",
    "{username}",
    new { controller = "Home", action = "Profile" }
    );
您可以指定所需的控制器和操作,只需在Home类的方法概要文件中使用参数用户名


您可以指定所需的控制器和操作,只需为Home类的方法概要文件的参数使用用户名。

您需要专门为此编写一个控制器,并创建如下路由:

routes.MapRoute(
  "UserPage", // Route name
  "{username}", // URL with parameters
  new { controller = "User", action = "Index", username = ""} // Parameter defaults
  );
有关更多详细信息,请参见此处:
)

您需要专门为此编写一个控制器,并创建一条如下路径:

routes.MapRoute(
  "UserPage", // Route name
  "{username}", // URL with parameters
  new { controller = "User", action = "Index", username = ""} // Parameter defaults
  );
有关更多详细信息,请参见此处: )

在您的Global.asax中

routes.MapRouteWithName(
    "routeUserProfile",
    "{username}",
    new { controller = "User", action = "Profile", username = "" });
在您的用户控制器中

public ActionResult Profile(string username) {
    //conditional logic to check if username is user
    // render user profile with special user-only stuff
    //else
    // render only generic stuff about user
}
在您的全球。尽快

routes.MapRouteWithName(
    "routeUserProfile",
    "{username}",
    new { controller = "User", action = "Profile", username = "" });
在您的用户控制器中

public ActionResult Profile(string username) {
    //conditional logic to check if username is user
    // render user profile with special user-only stuff
    //else
    // render only generic stuff about user
}

在global.asax文件中添加以下路由

        routes.MapRoute(
            "UsersRoute", // Route name
            "{username}", // URL with parameters
            new { controller = "Test", action = "Index", username = "" } 
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
        );
根据第一条路线添加以下控制器,如下所示

public class TestController : Controller
{       
    public ActionResult Index(string username )
    {
        var p = username;
        return View();
    }
}

在global.asax文件中添加以下路由

        routes.MapRoute(
            "UsersRoute", // Route name
            "{username}", // URL with parameters
            new { controller = "Test", action = "Index", username = "" } 
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
        );
根据第一条路线添加以下控制器,如下所示

public class TestController : Controller
{       
    public ActionResult Index(string username )
    {
        var p = username;
        return View();
    }
}

为了防止用户看到其他人的个人资料,只要检查该操作,如果他/她可以这样做

public ViewResult Index(string username)
{
  if (CanSeeOthersProfiles(username)) //your function to check currently logged user and his privileges
  {
     var model = new MyModel();
     //do your logic
     return View(model);
  }
  else
     return RedirectToAction("index", "home");
}

为了防止用户看到其他人的个人资料,只要检查该操作,如果他/她可以这样做

public ViewResult Index(string username)
{
  if (CanSeeOthersProfiles(username)) //your function to check currently logged user and his privileges
  {
     var model = new MyModel();
     //do your logic
     return View(model);
  }
  else
     return RedirectToAction("index", "home");
}

但是像这样,它认为任何url都是用户名,假设我想让用户访问www.example.com/accountsettings,它认为帐户设置是用户名,我想我也需要为此创建另一个路由。
routes.maprote(“AccountSettins”,“accountsettings”,new{controller=“AccountSettings”,action=“Index”});routes.MapRoute(“myRouteName”、“{username}”、new{controller=“Profile”、action=“Index”});routes.MapRoute(“Default”、//路由名称“{controller}/{action}/{id}”“,//带有参数new的URL{controller=“Home”,action=“Index”,id=urlparmeter.Optional}”//参数默认值
但它认为任何url都是用户名,假设我想让用户访问www.example.com/accountsettings,它认为帐户设置是用户名,我想我还需要为此创建另一个路由。
routes.maprote(“AccountSettins”,“accountsettings”,新的{controller=“AccountSettings”,action=“Index”});routes.MapRoute(“myRouteName”,“{username}”,新的{controller=“Profile”,action=“Index”});routes.MapRoute(“默认”,//路由名称“{controller}/{action}/{id}”,//带有参数new{controller=“Home”、action=“Index”、id=urlparmeter.Optional}//参数默认值