Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 具有自定义文本的asp.net mvc路由url_Asp.net Mvc_Routing - Fatal编程技术网

Asp.net mvc 具有自定义文本的asp.net mvc路由url

Asp.net mvc 具有自定义文本的asp.net mvc路由url,asp.net-mvc,routing,Asp.net Mvc,Routing,是否可以使用具有默认参数的自定义文字分隔符创建url context.MapRoute( "Forums_links", "Forum/{forumId}-{name}", new { area = "Forums", action = "Index", controller = "Forum" }, new[] { "Jami.Web.Areas.Forums.Controllers" }

是否可以使用具有默认参数的自定义文字分隔符创建url

context.MapRoute(
            "Forums_links",
            "Forum/{forumId}-{name}",
            new { area = "Forums", action = "Index", controller = "Forum" },
            new[] { "Jami.Web.Areas.Forums.Controllers" }
        );
我有这个,正如你们看到的,我用破折号将id和名称分开,这样我就可以有如下url:

/Forum/1-forum-name
而不是:

/Forum/1/forum-name

我发现问题是我使用了多个破折号。和路由引擎不知道哪一个分开。但总的来说,这不会改变我的问题,因为我无论如何都想使用多个破折号。

实现这一点的一种方法是将id和名称组合到同一个路由值中:

context.MapRoute(
            "Forums_links",
            "Forum/{forumIdAndName}",
            new { area = "Forums", action = "Index", controller = "Forum" },
            new[] { "Jami.Web.Areas.Forums.Controllers" }
        );
然后从中提取Id:

private static int? GetForumId(string forumIdAndName)
{
    int i = forumIdAndName.IndexOf("-");
    if (i < 1) return null;

    string s = forumIdAndName.Substring(0, i);

    int id;
    if (!int.TryParse(s, out id)) return null;

    return id;
}
私有静态int?GetForumId(用于umidName的字符串)
{
int i=forumIdAndName.IndexOf(“-”);
如果(i<1)返回null;
字符串s=forumidName.Substring(0,i);
int-id;
如果(!int.TryParse(s,out id))返回null;
返回id;
}

非常有趣的问题

我唯一能想到的办法就是和丹尼尔的很像,还有一个额外的功能

context.MapRoute(
    "Forums_links",
    "Forum/{forumIdAndName}",
    new { area = "Forums", action = "Index", controller = "Forum" },
    new { item = @"^\d+-(([a-zA-Z0-9]+)-)*([a-zA-Z0-9]+)$" } //constraint
    new[] { "Jami.Web.Areas.Forums.Controllers" }
);
这样,将与此路由匹配的唯一项目是以以下模式格式化的项目:

[one or more digit]-[zero or more repeating groups of string separated by dashes]-[final string]

在这里,您将使用Daniel posted方法解析ForumAndName参数中所需的数据。

+1约束子值,特别是Id是个好主意。我将编写类似正则表达式的
^\d+([a-zA-Z0-9]+)*$
,它稍微短一点,并匹配Id存在但名称不存在的URL(alla StackOverflow)。干杯