【问题标题】:Default dynamic + static route in asp.net MVC 5asp.net MVC 5中的默认动态+静态路由
【发布时间】:2015-01-18 10:29:55
【问题描述】:

当我运行我的应用程序时,我会在地址栏中看到以下 URL。

http://Localhost/

应用默认路由,

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

但我要设置的是,

当我运行我的应用程序时,我希望看到这样的 URL,

http://localhost/clientName/login  (I want to display this fixed URL on start up of application)

假设此时用户显示在登录屏幕上。并在登录屏幕上方显示“ClientName”。

现在我的要求是,用户应该能够修改这个“ClientName”。

例如。如果用户输入 John 作为 clientName,则在登录屏幕上方应显示 John。

我希望我清楚自己的要求。

现在我可以动态获取 clientName。为此,我设置了以下路线。

routes.MapRoute("Customers", "{customer}", new { controller = "Login", action = "Login" }, null);

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

我的登录控制器,

public ActionResult Login(string returnUrl, string customer)
{
    if(ClientExists(customer)) //Check against DB or list or any other variable
    {
        //Do some custom logic
    }
    ViewBag.ReturnUrl = returnUrl;
    return View();
} 

请点击此链接http://www.codeproject.com/Tips/825266/ASP-NET-MVC-Dynamic-Routing 获取更多参考。

有了这个,我在 Login actionMethod 的“returnUrl”参数中看不到任何值。

那么首先如何设置那个固定的 URL? second 如何从 URL 动态更改和获取 clientName(如果更改)?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing


    【解决方案1】:

    我只能帮你解决这个问题:I never see any value in "returnUrl" parameter of Login actionMethod.

    returnUrl 没有获得任何值,因为您要么使用了错误的绑定,要么没有全部使用。

    MVC 绑定如何工作?很简单。

    让我们来看看你的代码:

    public ActionResult Login(string returnUrl, string customer)

    当从任何地方调用此方法时,它会期望在调用 URL 中查找类似 @​​987654326@ 或 customer 的内容。

    假设我们称这条路径为:http://localhost:port/Login/Login。 这将出现在您的Login 方法中,它会尝试分析它并检查是否有任何参数随它一起发送。在这种情况下,没有发送任何参数,它不会将任何东西绑定到strings。

    但是,如果我们将此路径称为:http://localhost:port/Login/Login?returnUrl=something&customer=customerName,这将触发您拥有的确切 Login 方法,并将绑定正确的值。因此,在string returnUrl 中,您将拥有值“something”,而在string customer 中,您将拥有值“customerName”。

    【讨论】:

      【解决方案2】:

      尝试如下设置你的路线(我正在用VB写我的,但你应该能够轻松翻译):

      routes.MapRoute(
              name:="Customers",
              url:="Customers/{*pathInfo}",
              defaults:=New With {.controller = "Customers", .action = "Login"}
          )
      

      要访问它,只需访问:

      http://yoursite.com/Customers/John/JohnsHomePage

      然后,您的客户控制器将具有如下所示的登录方法:

      Function Login(ByVal pathInfo As String) As ActionResult
          ' Use your path info to extract your information
          ' It will only contain the part of the URL past the /Customers path
          ' Parse pathInfo to get returnUrl
      
          ' Do your page work
          Response.Redirect(returnUrl)
      End Function
      

      将客户 url 路径更改为登录或其他内容。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-24
        • 2011-07-15
        • 1970-01-01
        • 2010-11-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-23
        • 2017-07-08
        相关资源
        最近更新 更多