【问题标题】:Url redirect with the username in asp.net使用 asp.net 中的用户名重定向 URL
【发布时间】:2015-09-04 06:32:25
【问题描述】:

我需要链接是这种格式http://localhost:32136/username/HomePage

例如:-https://www.facebook.com/xxxyyyy

请给我建议如何构建 URL 的链接。我已将 route.config 配置为

routes.MapRoute(
 name: "Profile",
    url: "{username}",
    defaults: new { controller = "Account", action = "Profile" },
    constraints: new { username = "Vinoth" }
);

【问题讨论】:

  • 您将使用哪个列表?你能在这里放更多代码吗?
  • localhost:32136/username/HomePage 用户名将是vinoth 例如怎么做
  • 我认为这是 URL,您将从列表中获取记录?
  • 请重新检查我的问题现在是否已编辑..

标签: asp.net routing url-routing


【解决方案1】:

试试这个。

RouteConfig.cs

routes.MapRoute(
    name: "ProfileRoute",
    url: "{username}/{action}",
    defaults: new { controller = "Account", action = "Profile" },
);

AccountController.cs

public ActionResult Profile(string username)
{
   if(username != "Vinoth")
       return HttpNotFound();   
   return View(username);
}

【讨论】:

  • RouteConfig.cs routes.MapRoute( name: "ProfileRoute", url: "{username}/{action}", 默认值: new { controller = "Account", action = "Profile" } ) ; routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { action = "Index", id = UrlParameter.Optional } ); AccountController in apicontroller public IHttpActionResult Profile(string username) { if (username != "Vinoth") return NotFound();返回确定(用户名); }
  • 我在 webapi 控制器中创建了 AccountController.cs .. 使用它很好
猜你喜欢
  • 1970-01-01
  • 2017-01-12
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
相关资源
最近更新 更多