ASP。NET MVC自定义搜索路由

时间:2022-08-26 20:49:25

Here is my scenario. For the example lets say that I need to return a list of cars based on a search criteria. I would like to have a single View to display the results since the output will be the same, but I need several ways of getting there. For instance, I may have a Form with a textbox to search by year. I may have another separate page that contains a hyperlink for all red, Toyota cars. How do I handle these multiple scenarios in the same View and Controller. My dilemma is that the search could contain several options… year, make, model, etc but I don’t know where to put them.

这是我的场景。例如,假设我需要根据搜索条件返回汽车列表。我希望有一个视图来显示结果,因为输出是相同的,但是我需要几种方法来实现。例如,我可能有一个带有文本框的表单,可以按年搜索。我可能还有另一个单独的页面,它包含了所有红色丰田汽车的超链接。如何在相同的视图和控制器中处理这些不同的场景。我的难题是搜索可能包含几个选项……年份、制作、模型等等,但我不知道把它们放在哪里。

What is the best approach for this? Should I define the parameters in the routing or go with query strings, etc?

最好的方法是什么?我应该在路由中定义参数还是使用查询字符串等等?

3 个解决方案

#1


78  

Option 1

选项1

Of course you always can choose the way of /car/search/?vendor=Toyota&color=Red&model=Corola and I think it will be good for you.

当然你总是可以选择道路/汽车/搜索/?供应商=Toyota&color=Red&model=Corola,我认为这对你有好处。

routes.MapRoute(
    "CarSearch",
    "car/search",
    new { controller = "car", action = "search" }
);

You can get params from Request.Params in action in this case.

您可以从请求中获得参数。在这种情况下,Params在行动。

Option 2

选项2

Or you can define params in the routing table, but AFAIK it will be required to make a set of rules for all possible combinations, because an order of the params matter, for example:

或者可以在路由表中定义params,但是必须为所有可能的组合制定一组规则,因为params的顺序很重要,例如:

        routes.MapRoute(
            "CarSearch1",
            "car/search/vendor/{vendor}/color/{color}/model/{model}",
            new {controller = "car", action = "search"}
        );

        routes.MapRoute(
            "CarSearch2",
            "car/search/color/{color}/vendor/{vendor}/model/{model}",
            new {controller = "car", action = "search"}
        );

        routes.MapRoute(
            "CarSearch3",
            "car/search/model/{model}/color/{color}/vendor/{vendor}",
            new {controller = "car", action = "search"}
        );

... an so on. It's true if you are going with the standard MvcRouteHandler.

…一个等等。如果您使用的是标准的MvcRouteHandler,那么这是正确的。

But it was an easy ways :)

但这是一个简单的方法:

Option 3

选项3

The hard, but, I think, most elegant way, is to make your own IRouteHandler implementation - it will give you much more flexibility in params order. But again, its a hard way, dont go with it if you have a simple app. So, just for example of how to make it this way (very simple example):

但是,我认为,最优雅的方法是,创建您自己的IRouteHandler实现——它将使您在params订单中获得更大的灵活性。但是,如果你有一个简单的应用,那就别用它了。

Add new route to the list of routes:

将新路线添加到路线列表:

routes.Add
    (
        new Route
            (
                "car/search/{*data}",
                new RouteValueDictionary(new {controller = "car", action = "search", data = ""}),
                new MyRouteHandler()
            )
    );

Add classes that will tweak the standard request processing chain:

添加将调整标准请求处理链的类:

class MyRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MyHttpHandler(requestContext);
    }
}

class MyHttpHandler : MvcHandler
{
    public MyHttpHandler(RequestContext requestContext) : base(requestContext)
    {
    }

    protected override void ProcessRequest(HttpContextBase httpContext)
    {
        IController controller = new CarController();
        (controller as Controller).ActionInvoker = new MyActionInvoker();
        controller.Execute(RequestContext);
    }
}

class MyActionInvoker : ControllerActionInvoker
{
    protected override ActionResult InvokeActionMethod(MethodInfo methodInfo, IDictionary<string, object> parameters)
    {
        // if form of model/{model}/color/{color}/vendor/{vendor}
        var data = ControllerContext.RouteData.GetRequiredString("data");
        var tokens = data.Split('/');

        var searchParams = new Dictionary<string, string>();
        for (var i = 0; i < tokens.Length; i++)
        {
            searchParams.Add(tokens[i], tokens[++i]);
        }

        parameters["searchParams"] = searchParams;

        return base.InvokeActionMethod(methodInfo, parameters);
    }
}

In controller:

控制器:

public ActionResult Search(IDictionary<string, string> searchParams)
{
    ViewData.Add
        (
            // output 'model = Corola, color = red, vendor = Toyota'
            "SearchParams",
            string.Join(", ", searchParams.Select(pair => pair.Key + " = " + pair.Value).ToArray())
        );
    return View();
}

And it will work with any search parameters order:

它可以根据任何搜索参数排序:

/car/search/vendor/Toyota/color/red/model/Corola
/car/search/color/red/model/Corola/vendor/Toyota
/car/search/model/Corola/color/red/vendor/Toyota

But also dont forget to make a link generation logic, because Html.ActionLink and Html.RenderLink will not give you url in pretty form of /car/search/model/Corola/color/red/vendor/Toyota, so you'll need to make a custom link generator.

但也别忘了做一个链接生成逻辑,因为Html。ActionLink和Html。RenderLink不会以漂亮的形式给你url /car/search/model/Corola/color/red/vendor/Toyota,所以你需要做一个自定义链接生成器。

So, if you need a really flexible routing - you'd better go with this hard way :)

因此,如果你需要一个非常灵活的路由——你最好采用这种艰难的方式:)

#2


0  

Each method (action) on the controller would take different parameters, but create the same collection of search results. Then, each would

控制器上的每个方法(操作)将采用不同的参数,但创建相同的搜索结果集合。然后,每个人都

return View("SearchResult", searchResultCollection);

They all use the same view, SearchResult.aspx.

它们都使用相同的视图SearchResult.aspx。

#3


0  

Something along these lines should do what you're after. Notice how there's two different action methods, but both of them return a call to DisplayResults() - so they end up using the same view, with different ViewData.

沿着这条线的东西应该做你想做的事。注意这里有两个不同的操作方法,但它们都返回对DisplayResults()的调用——因此它们最终使用相同的视图,使用不同的视图数据。

public class SearchController : Controller {

    public ActionResult ByColor(Color[] colors) {
         List<Car> results = carRepository.FindByColor(colors);
         return(DisplayResults(result));
    }

    public ActionResult ByMake(string make) {
         List<Car> results = carRepository.FindByMake(make);
         return(DisplayResults(results));
    }

    private ActionResult DisplayResults(IList<Car> results) {

        // Here we explicitly return the view /Views/Search/Results.aspx
        // by specifying the view name in the call to View();
        return(View("Results", results));
    }
}

#1


78  

Option 1

选项1

Of course you always can choose the way of /car/search/?vendor=Toyota&color=Red&model=Corola and I think it will be good for you.

当然你总是可以选择道路/汽车/搜索/?供应商=Toyota&color=Red&model=Corola,我认为这对你有好处。

routes.MapRoute(
    "CarSearch",
    "car/search",
    new { controller = "car", action = "search" }
);

You can get params from Request.Params in action in this case.

您可以从请求中获得参数。在这种情况下,Params在行动。

Option 2

选项2

Or you can define params in the routing table, but AFAIK it will be required to make a set of rules for all possible combinations, because an order of the params matter, for example:

或者可以在路由表中定义params,但是必须为所有可能的组合制定一组规则,因为params的顺序很重要,例如:

        routes.MapRoute(
            "CarSearch1",
            "car/search/vendor/{vendor}/color/{color}/model/{model}",
            new {controller = "car", action = "search"}
        );

        routes.MapRoute(
            "CarSearch2",
            "car/search/color/{color}/vendor/{vendor}/model/{model}",
            new {controller = "car", action = "search"}
        );

        routes.MapRoute(
            "CarSearch3",
            "car/search/model/{model}/color/{color}/vendor/{vendor}",
            new {controller = "car", action = "search"}
        );

... an so on. It's true if you are going with the standard MvcRouteHandler.

…一个等等。如果您使用的是标准的MvcRouteHandler,那么这是正确的。

But it was an easy ways :)

但这是一个简单的方法:

Option 3

选项3

The hard, but, I think, most elegant way, is to make your own IRouteHandler implementation - it will give you much more flexibility in params order. But again, its a hard way, dont go with it if you have a simple app. So, just for example of how to make it this way (very simple example):

但是,我认为,最优雅的方法是,创建您自己的IRouteHandler实现——它将使您在params订单中获得更大的灵活性。但是,如果你有一个简单的应用,那就别用它了。

Add new route to the list of routes:

将新路线添加到路线列表:

routes.Add
    (
        new Route
            (
                "car/search/{*data}",
                new RouteValueDictionary(new {controller = "car", action = "search", data = ""}),
                new MyRouteHandler()
            )
    );

Add classes that will tweak the standard request processing chain:

添加将调整标准请求处理链的类:

class MyRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new MyHttpHandler(requestContext);
    }
}

class MyHttpHandler : MvcHandler
{
    public MyHttpHandler(RequestContext requestContext) : base(requestContext)
    {
    }

    protected override void ProcessRequest(HttpContextBase httpContext)
    {
        IController controller = new CarController();
        (controller as Controller).ActionInvoker = new MyActionInvoker();
        controller.Execute(RequestContext);
    }
}

class MyActionInvoker : ControllerActionInvoker
{
    protected override ActionResult InvokeActionMethod(MethodInfo methodInfo, IDictionary<string, object> parameters)
    {
        // if form of model/{model}/color/{color}/vendor/{vendor}
        var data = ControllerContext.RouteData.GetRequiredString("data");
        var tokens = data.Split('/');

        var searchParams = new Dictionary<string, string>();
        for (var i = 0; i < tokens.Length; i++)
        {
            searchParams.Add(tokens[i], tokens[++i]);
        }

        parameters["searchParams"] = searchParams;

        return base.InvokeActionMethod(methodInfo, parameters);
    }
}

In controller:

控制器:

public ActionResult Search(IDictionary<string, string> searchParams)
{
    ViewData.Add
        (
            // output 'model = Corola, color = red, vendor = Toyota'
            "SearchParams",
            string.Join(", ", searchParams.Select(pair => pair.Key + " = " + pair.Value).ToArray())
        );
    return View();
}

And it will work with any search parameters order:

它可以根据任何搜索参数排序:

/car/search/vendor/Toyota/color/red/model/Corola
/car/search/color/red/model/Corola/vendor/Toyota
/car/search/model/Corola/color/red/vendor/Toyota

But also dont forget to make a link generation logic, because Html.ActionLink and Html.RenderLink will not give you url in pretty form of /car/search/model/Corola/color/red/vendor/Toyota, so you'll need to make a custom link generator.

但也别忘了做一个链接生成逻辑,因为Html。ActionLink和Html。RenderLink不会以漂亮的形式给你url /car/search/model/Corola/color/red/vendor/Toyota,所以你需要做一个自定义链接生成器。

So, if you need a really flexible routing - you'd better go with this hard way :)

因此,如果你需要一个非常灵活的路由——你最好采用这种艰难的方式:)

#2


0  

Each method (action) on the controller would take different parameters, but create the same collection of search results. Then, each would

控制器上的每个方法(操作)将采用不同的参数,但创建相同的搜索结果集合。然后,每个人都

return View("SearchResult", searchResultCollection);

They all use the same view, SearchResult.aspx.

它们都使用相同的视图SearchResult.aspx。

#3


0  

Something along these lines should do what you're after. Notice how there's two different action methods, but both of them return a call to DisplayResults() - so they end up using the same view, with different ViewData.

沿着这条线的东西应该做你想做的事。注意这里有两个不同的操作方法,但它们都返回对DisplayResults()的调用——因此它们最终使用相同的视图,使用不同的视图数据。

public class SearchController : Controller {

    public ActionResult ByColor(Color[] colors) {
         List<Car> results = carRepository.FindByColor(colors);
         return(DisplayResults(result));
    }

    public ActionResult ByMake(string make) {
         List<Car> results = carRepository.FindByMake(make);
         return(DisplayResults(results));
    }

    private ActionResult DisplayResults(IList<Car> results) {

        // Here we explicitly return the view /Views/Search/Results.aspx
        // by specifying the view name in the call to View();
        return(View("Results", results));
    }
}