默认动态+静态路由在asp.net MVC 5 [英] Default dynamic + static route in asp.net MVC 5

查看:155
本文介绍了默认动态+静态路由在asp.net MVC 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我跑我的应用程序得到下面的地址栏中的URL。

When I run my application I get following URL in address bar.

http://Localhost/

应用程序的默认路由,

application default Routing,

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

但我想设置为

当我跑我的应用程序我想看看URL这样,

When I run my application I want to see URL like this,

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

让我们在这个时刻用户显示与登录界面说。以上的登录屏幕CLIENTNAME显示。

Lets say at this moment user is shown with Login screen. and above the login screen "ClientName" is displayed.

现在我的要求是,用户应该能够锤炼这个CLIENTNAME。

Now my requirement is, user should be able to temper this "ClientName".

有关如。如果用户输入约翰为CLIENTNAME,登录屏幕上面应该显示约翰。

For eg. If user enters John as a clientName, above the login screen it should display John.

我希望我和我的要求明确。

I hope I'm clear with my requirements.

现在我能够动态获取CLIENTNAME。为我设置下面的路线。

for now I'm able to get clientName dynamically. for that I've set following route.

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 }
            );

我的LoginController,

My LoginController,

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.$c$cproject.com/Tips/825266/ASP-NET-MVC-动态路由了解参考。

Please follow this link http://www.codeproject.com/Tips/825266/ASP-NET-MVC-Dynamic-Routing for more reference.

有了这个,我从来没有看到在登录actionMethod的RETURNURL参数的任意值。

With this, I never see any value in "returnUrl" parameter of Login actionMethod.

因此​​,首先如何设置固定的网址是什么?
第二如何我可以改变,并得到CLIENTNAME(如果更改)动态地从URL?

So first how to set that fixed URL? second How can I change and get clientName (if changed) dynamically from URL?

推荐答案

尝试设置你的路线如下(我在VB写我的,但你应该能够很容易地翻译):

Try setting your route as follows (I am writing mine in VB, but you should be able to translate easily):

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

要访问它,只需访问:

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

然后,您的客户控制器将有一个登录方法,像这样:

Then, your Customers Controller would have a Login method like so:

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路径登录或别的东西。

Change the Customers url path to Login or something else.

这篇关于默认动态+静态路由在asp.net MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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