ASP净MVC路由:只用绳子ID地址 [英] ASP .Net MVC Routing: Url only with string ID

查看:144
本文介绍了ASP净MVC路由:只用绳子ID地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单的问题,但我无法找到这个答案:

very simple question but I couldn't find an answer for this:

我有默认的{控制器} / {行动}在我的全球ASAX /(编号)模式。

I have the default {controller}/{action}/{id} pattern in my global asax.

我也需要一些能给我像www.example.com/microsoft或www.example.com/apple而微软和苹果都存储在数据库ID。使用默认模式将是:www.example.com/brands/detail/microsoft

I need also something will give me something like www.example.com/microsoft or www.example.com/apple while microsoft and apple are id stored in database. Using the default pattern would be: www.example.com/brands/detail/microsoft

任何想法的格局应该如何?
我想:{ID},并设置控制器和行动,以品牌和细节它的工作为我的需要,但废墟所有其他模式

any idea how the pattern should be? i tried: {id} and set the controller and action to "brands" and "detail" it work to serve my need but ruins all others pattern.

感谢

推荐答案

您路线次序的问题。因此,创建一个第一路由定义它处理所有可用的控制器,然后提一句,这将处理请求的其余部分。在那里,你将处理 www.yousite.com/apple 样的要求

Your route order matters. So create a first route definition which handles all available controllers and then mention one which will handle the rest of the requests. There you will handle the www.yousite.com/apple kind of request

routes.MapRoute("Default",  // Route name
                  "{controller}/{action}/{id}", // URL with parameters
                    new { controller = "Home", action = "Index", id = "" },
                    new { controller = new FromValuesListConstraint("Home", "Account","OtherOne") }
                );

 // to handle personalize user url
routes.MapRoute("user","{url}", new {controller="Home",action="Profile",url = "" });

现在创建 FromValues​​ListContraint 称为一个新的类从IRouteConstraint继承

Now create a new class called FromValuesListContraint which inherit from IRouteConstraint

public class FromValuesListConstraint : IRouteConstraint
{
    private string[] _values;

    public FromValuesListConstraint(params string[] values)
    {
        this._values = values; 
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName,
    RouteValueDictionary values, RouteDirection routeDirection)
    {
        // Get the value called "parameterName" from the
        // RouteValueDictionary called "value"

        string value = values[parameterName].ToString();

        // Return true is the list of allowed values contains
        // this value.

        for (int i = 0; i < _values.Length; i++)
            if (SContains(_values[i], value, StringComparison.OrdinalIgnoreCase ))
                return true;

        return false;
    }    

    public bool SContains(string source, string toCheck, StringComparison comp)
    {
        return source.IndexOf(toCheck, comp) >= 0;
    }
}

让你的简介在家操作方法读取参数值,并从数据库中获取数据。

Have your Profile action method in Home read the parameter value and get data from your database.

 public ActionResult Profile(string url)
 {
    //url variable will have apple or microsoft . You may get data from db and return a view now.
 }

所以每当收到请求时,它会检查是否可用一个控制器(你在第一路由定义传递到FromValues​​ListContraint类的构造函数),如果有那么它会去该路由,否则,它会去作为第二条路线中提到的一般(默认)的路由。

So whenever a request comes, It will check whether it is a controller available ( which you passed into the FromValuesListContraint class constructor in your first route definition), if available then it will go for that routing, else, it will go for the general (default) route mentioned as the second route.

在这个例子中,家庭,帐户和OtherOnes是我提供的控制器。只要添加一个新的控制器到您的项目,你想这增加FromValues​​ListConstraint类构造函数的constrctor。

In this example, Home, Account and OtherOnes are my available controllers. whenever you add a new controller to your project, you want to add that to the constrctor of FromValuesListConstraint class constructor.

简单地说,它的工作原理像抓住了一些具体的例外和去一般的异常,如果他们没有被发现! :)(只是一个例子就明白了)

Simply saying it works like Catching some specific exception and going to the general exception if none of them are caught! :) (just an example to understand)

这篇关于ASP净MVC路由:只用绳子ID地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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