需要帮助canonicalizing在asp.net mvc的一个HTTP GET形式 [英] need help canonicalizing an HTTP GET form in asp.net mvc

查看:201
本文介绍了需要帮助canonicalizing在asp.net mvc的一个HTTP GET形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在供应3目的的asp.net的MVC网站上的表单:分页,排序和搜索。这些项目都应该以相同的形式呈现,因为返回正确的搜索结果依赖于来自所有3个方面的变量。我正在试图做的是移动的参数进行查询字符串,并把它们在一个规范的URL。

我几乎没有,这里有我的3路由配置到目前为止(使用T4MVC的区域,控制器和动作名):

  context.MapRoute(NULL,
    我的区域/我的小部件/搜索/ {}大小 - 结果-MAX /页面级{PAGE} /顺序逐{}排序,
    新
    {
        面积= MVC.MyArea.Name,
        控制器= MVC.MyArea.MyWidgets.Name,
        行动= MVC.MyArea.MyWidgets.ActionNames.Search,
        页= UrlParameter.Optional,
        大小= UrlParameter.Optional,
        排序= UrlParameter.Optional,
    }
);context.MapRoute(NULL,
    我的区域/我的小部件/。规范化搜索
    新
    {
        面积= MVC.MyArea.Name,
        控制器= MVC.MyArea.MyWidgets.Name,
        行动= MVC.MyArea.MyWidgets.ActionNames.CanonicalizeSearch,
    }
);context.MapRoute(NULL,
    我的区域/我的小部件,
    新
    {
        面积= MVC.MyArea.Name,
        控制器= MVC.MyArea.MyWidgets.Name,
        行动= MVC.MyArea.MyWidgets.ActionNames.CanonicalizeSearch,
    }
);

视图中的形式提交给CanonicalizeSearch路线,使用的语法如下:

  @using(Html.BeginForm(MVC.MyArea.MyWidgets.CanonicalizeSearch()
    FormMethod.Get))

在MyWidgetsController,有2个动作方法:

  [ActionName(。规范化搜索)]
公共虚拟RedirectToRouteResult CanonicalizeSearch(字符串关键字,
    INT页面= 1,INT大小= 10,串排序=标题 - 升序)
{
    VAR的结果= RedirectToRoutePermanent(新
    {
        面积= MVC.MyArea.Name,
        控制器= MVC.MyArea.MyWidgets.Name,
        行动= MVC.MyArea.MyWidgets.ActionNames.Search,
        页=页,
        大小=,
        排序=排序,
        关键字=关键字,
    });
    返回结果;
}[ActionName(搜索)
公共虚拟的ViewResult搜索(字符串关键字,
    INT页面= 1,INT大小= 10,串排序=标题 - 升序)
{
    // code进行查询
    返回查看(模型);
}

这适用于所有移动查询字符串变量到一个规范化的路线除了关键字。如果我添加关键字参数,第一个路线,CanonicalizeSearch操作只重定向到搜索操作时的关键字不为空,空,或空白。这是没有好处,因为它使浏览页面的结果不可能在没有输入的关键字。

我想我已经尝试了一切 - 让关键字控制器中的默认值,并补充,增加了关键字其他3个参数第4路线等,但我似乎唯一的办法得到这个工作是保持关键字作为查询字符串参数。 (其实我能得到它prepending下划线在CanonicalizeSearch关键词,并在搜索剥离它去上班,但那是pretty哈克)。

任何帮助吗?


解决方案

我想我在一个更好的解决这个跌跌撞撞试图解决另一个问题。

说在关键字中的我的搜索条件有人类型。提交引起该CanonicalizeSearch方法路线路径

  /我的领域/我的小部件/搜索/ 10-结果单页/页-1 /
    为了按标题 - 升序/我的%20search%20terms

那些20%符号是烦人。我宁愿网址是这样的:

  /我的领域/我的小部件/搜索/ 10-结果单页/页-1 /
    订购按标题 - 升序/我-搜索条件

我可以用下面的(注意到一个临时重定向从永久的变化)完成此

  [ActionName(。规范化搜索)]
公共虚拟RedirectToRouteResult CanonicalizeSearch(字符串关键字,
    INT页面= 1,INT大小= 10,串排序=标题 - 升序)
{
    VAR的结果= RedirectToRoute(新
    {
        面积= MVC.MyArea.Name,
        控制器= MVC.MyArea.MyWidgets.Name,
        行动= MVC.MyArea.MyWidgets.ActionNames.Search,
        页=页,
        大小=,
        排序=排序,
        关键字=(string.IsNullOrWhiteSpace(关键字))
            ? 无关键字:keyword.Replace('',' - '),
    });
    TempData的[关键字] =关键字;
    返回结果;
}[ActionName(搜索)
公共虚拟的ViewResult搜索(字符串关键字,
    INT页面= 1,INT大小= 10,串排序=标题 - 升序)
{
    关键字= TempData的[关键字]作为字符串?关键词;
    // code进行查询
    返回查看(模型);
}

这既解决了我这里张贴问题和去除20%的符号。每当关键字是空空或空格,它将使该网址

  /我的领域/我的小部件/搜索/ 10-结果单页/页-1 /
    订购按标题 - 升序/无关键字

...和路线会一直匹配。

I have a form in an asp.net mvc site that serves 3 purposes: paging, sorting, and searching. These items should all be rendered in the same form, since returning the correct search results depends on variables from all 3 aspects. What I'm trying to do is move the parameters out of the querystring and put them in a canonical URL.

I'm almost there, here are my 3 route configurations so far (using T4MVC for area, controller, and action names):

context.MapRoute(null,
    "my-area/my-widgets/search/{size}-results-max/page-{page}/order-by-{sort}",
    new
    {
        area = MVC.MyArea.Name,
        controller = MVC.MyArea.MyWidgets.Name,
        action = MVC.MyArea.MyWidgets.ActionNames.Search,
        page = UrlParameter.Optional,
        size = UrlParameter.Optional,
        sort = UrlParameter.Optional,
    }
);

context.MapRoute(null,
    "my-area/my-widgets/canonicalize-search",
    new
    {
        area = MVC.MyArea.Name,
        controller = MVC.MyArea.MyWidgets.Name,
        action = MVC.MyArea.MyWidgets.ActionNames.CanonicalizeSearch,
    }
);

context.MapRoute(null,
    "my-area/my-widgets",
    new
    {
        area = MVC.MyArea.Name,
        controller = MVC.MyArea.MyWidgets.Name,
        action = MVC.MyArea.MyWidgets.ActionNames.CanonicalizeSearch,
    }
);

The form in the view submits to the CanonicalizeSearch route, using this syntax:

@using (Html.BeginForm(MVC.MyArea.MyWidgets.CanonicalizeSearch(), 
    FormMethod.Get))

In the MyWidgetsController, there are 2 action methods:

[ActionName("canonicalize-search")]
public virtual RedirectToRouteResult CanonicalizeSearch(string keyword, 
    int page = 1, int size = 10, string sort = "Title-Ascending")
{
    var result = RedirectToRoutePermanent(new
    {
        area = MVC.MyArea.Name,
        controller = MVC.MyArea.MyWidgets.Name,
        action = MVC.MyArea.MyWidgets.ActionNames.Search,
        page = page,
        size = size,
        sort = sort,
        keyword = keyword,
    });
    return result;
}

[ActionName("search")]
public virtual ViewResult Search(string keyword, 
    int page = 1, int size = 10, string sort = "Title-Ascending")
{
    // code to perform query
    return View(model);
}

This works for moving all querystring variables into a canonicalized route except for the keyword. If I add a keyword parameter to the first route, the CanonicalizeSearch action only redirects to the Search action when keyword is not null, empty, or whitespace. This is no good as it makes browsing page results impossible when there is no keyword entered.

I think I've tried everything -- giving the keyword a default value in the controller, adding a 4th route that adds keyword to the other 3 parameters, etc. However the only way I can seem get this to work is by keeping keyword as a querystring parameter. (Actually I can get it to work by prepending an underscore to the keyword in CanonicalizeSearch and stripping it off in Search, but that's pretty hacky).

Any help?

解决方案

I think I stumbled on a better solution to this by trying to solve another problem.

Say someone types in "my search terms" in the keyword box. Submitting that causes the CanonicalizeSearch method to route to the path:

/my-area/my-widgets/search/10-results-per-page/page-1/
    order-by-Title-Ascending/my%20search%20terms

Those %20 symbols are annoying. I would rather the URL look like this:

/my-area/my-widgets/search/10-results-per-page/page-1/
    order-by-Title-Ascending/my-search-terms

I can accomplish this with the following (note the change from a permanent to a temporary redirect):

[ActionName("canonicalize-search")]
public virtual RedirectToRouteResult CanonicalizeSearch(string keyword, 
    int page = 1, int size = 10, string sort = "Title-Ascending")
{
    var result = RedirectToRoute(new
    {
        area = MVC.MyArea.Name,
        controller = MVC.MyArea.MyWidgets.Name,
        action = MVC.MyArea.MyWidgets.ActionNames.Search,
        page = page,
        size = size,
        sort = sort,
        keyword = (string.IsNullOrWhiteSpace(keyword)) 
            ? "no-keywords" : keyword.Replace(' ', '-'),
    });
    TempData["keyword"] = keyword;
    return result;
}

[ActionName("search")]
public virtual ViewResult Search(string keyword, 
    int page = 1, int size = 10, string sort = "Title-Ascending")
{
    keyword = TempData["keyword"] as string ?? keyword;
    // code to perform query
    return View(model);
}

This solves both the question I posted here and the removal of the %20 symbols. Whenever the keyword is null empty or whitespace, it will render the URL

/my-area/my-widgets/search/10-results-per-page/page-1/
    order-by-Title-Ascending/no-keywords

... and the route will always match.

这篇关于需要帮助canonicalizing在asp.net mvc的一个HTTP GET形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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