ASP.NET MVC unique url routing

允我心安 提交于 2020-01-03 02:54:29

问题


So, I've read through tutorials and books about MVC routing as well as played with it on my projects and come to a pretty solid understanding of how to use it to accomplish what I want to with it.

But, I'm up against something I can't quite figure out yet.

What I want to accomplish is a unique url for each client that doesn't look like "http://mysite.com/client/1". This url would take the browser to the Client Controller, Index action, ClientId = 1...obviously.

What I'd like to do is have a URL like "http://mysite.com/Acme" that would do a database lookup to figure out which client has the unique name of "Acme", and then redirect the request to the Client Controller, Index view and set the ClientId to whatever it is on the client with the name 'Acme'.

The default route keeps catching it and can't handle it.

Any ideas?


回答1:


I recommend using an Global Action Filter to accomplish this or you can create a route with a static path that will route to your lookup controller (e.g., /lookup/{companyname} will route to your database lookup controller).




回答2:


How about "http://www.mysite.com/Clients/{ClientName}"

routes.MapRoute(null, "Clients/{ClientName}", new{controller = "Clients", action = "Index"};

public class ClientsController : Controller
{
    public ActionResult Index(string clientName)
    {
        var id = Db.GetClientIdBy(clientName);

        // do your redirect...
    }
}    

Or have I missed the point?



来源:https://stackoverflow.com/questions/14061365/asp-net-mvc-unique-url-routing

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