ASP.NET MVC路由 - 使用可选参数和友好URL进行路由

时间:2023-01-03 11:02:13

I have a search action method which accepts at this time 5 optional parameters for its search criteria. My home page has a quick search button which submits a GET request to the search action method. I have everything working fine, however my urls are being generated using standard querystring syntax (?x=x&y=y).

我有一个搜索操作方法,此时它接受5个可选参数作为其搜索条件。我的主页有一个快速搜索按钮,用于向搜索操作方法提交GET请求。我的一切工作正常,但我的网址是使用标准的查询字符串语法生成的(?x = x&y = y)。

I would like to expose friendly urls but the only way I see possible using the routing system would be to add every single permutation posible since all the parameters are optional, which does not sound very elegant.

我想公开友好的网址,但我认为可能使用路由系统的唯一方法是添加每个单独的排列,因为所有参数都是可选的,听起来不是很优雅。

So are there any other routes (no pun intended :) ) I can take? Possible a Url rewrite module?

所以有没有其他路线(没有双关语:) :)我可以采取?可能是Url重写模块?

Any help would be greatly appreciated.

任何帮助将不胜感激。

2 个解决方案

#1


0  

Option 1: Live with querystrings. There is nothing that great about friendly Urls on a search page.

选项1:与查询字符串一起生活。在搜索页面上,友好的Urls没什么了不起的。

/search?q=url+routing

Option 2: Allow at most one optional parameter to be part of the "friendly" Url. Any additional parameters must be passed in querystrings. For example:

选项2:允许最多一个可选参数成为“友好”URL的一部分。必须在查询字符串中传递任何其他参数。例如:

/search/query/url+routing (1 parameter)
/search/query/url+routing?tagged=asp.net (2 parameters)
/search/query/url+routing?tagged=asp.net&sort=date (3 parameters)

#2


0  

You can setup a route if you're okay with a url such as http://mysite.com/search/{term-1}/{term-n}/{term-n+1}:

如果您使用http://mysite.com/search/{term-1}/{term-n}/{term-n+1}等网址,则可以设置路线:

routes.MapRoute(
    name: "SearchRoute",
    url: "search/{*terms}",
    defaults: new { controller = "MySearchController", action = "MySearchAction" }
);

In your action, you'll take in a string that will be / delimited with all of your search terms. (The URL might not make much sense, but it is something you can use.)

在您的操作中,您将接收将使用所有搜索字词/分隔的字符串。 (URL可能没有多大意义,但您可以使用它。)

public class SearchController : Controller
{
    public ActionResult SearchAction (string terms)
    {
        foreach (var term in terms.Split ('/'))
        {
            // Do something
        }
    }
}

#1


0  

Option 1: Live with querystrings. There is nothing that great about friendly Urls on a search page.

选项1:与查询字符串一起生活。在搜索页面上,友好的Urls没什么了不起的。

/search?q=url+routing

Option 2: Allow at most one optional parameter to be part of the "friendly" Url. Any additional parameters must be passed in querystrings. For example:

选项2:允许最多一个可选参数成为“友好”URL的一部分。必须在查询字符串中传递任何其他参数。例如:

/search/query/url+routing (1 parameter)
/search/query/url+routing?tagged=asp.net (2 parameters)
/search/query/url+routing?tagged=asp.net&sort=date (3 parameters)

#2


0  

You can setup a route if you're okay with a url such as http://mysite.com/search/{term-1}/{term-n}/{term-n+1}:

如果您使用http://mysite.com/search/{term-1}/{term-n}/{term-n+1}等网址,则可以设置路线:

routes.MapRoute(
    name: "SearchRoute",
    url: "search/{*terms}",
    defaults: new { controller = "MySearchController", action = "MySearchAction" }
);

In your action, you'll take in a string that will be / delimited with all of your search terms. (The URL might not make much sense, but it is something you can use.)

在您的操作中,您将接收将使用所有搜索字词/分隔的字符串。 (URL可能没有多大意义,但您可以使用它。)

public class SearchController : Controller
{
    public ActionResult SearchAction (string terms)
    {
        foreach (var term in terms.Split ('/'))
        {
            // Do something
        }
    }
}