Default dynamic + static route in asp.net MVC 5

杀马特。学长 韩版系。学妹 提交于 2020-01-06 14:41:31

问题


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

But what I want to set is,

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)

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

Now my requirement is, user should be able to temper this "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.

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

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

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

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

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


回答1:


I can help you only with this: I never see any value in "returnUrl" parameter of Login actionMethod.

You don't get any values to the returnUrl because you either use the binding wrong, or don't use it all.

How does the MVC binding work? Pretty simple.

Let's take the code you have:

public ActionResult Login(string returnUrl, string customer)

This method will expect when it is called from anywhere to look for something like returnUrl or customer in the calling URL.

Let's imagine we call this path: http://localhost:port/Login/Login . This will come to your Login method, and it will try to analyze it and check if there are any parameters sent with it. In this case, there are no parameters sent, and it won't bind anything to the strings.

But, if we call this path: http://localhost:port/Login/Login?returnUrl=something&customer=customerName , this will trigger the exact Login method you have and will bind the proper values. So in the string returnUrl you will have the value "something", and in the string customer you will have the value "customerName".




回答2:


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

To access it, simply access:

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

Change the Customers url path to Login or something else.



来源:https://stackoverflow.com/questions/28008687/default-dynamic-static-route-in-asp-net-mvc-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!