Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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/1/visual-studio-2008/2.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
C# Url路由,将id从数据库更改为名称_C#_Asp.net_Webforms - Fatal编程技术网

C# Url路由,将id从数据库更改为名称

C# Url路由,将id从数据库更改为名称,c#,asp.net,webforms,C#,Asp.net,Webforms,不确定这是否是最佳解决方案,但假设我有这样的url ?id=2 Id取自数据库,如何使其显示 /<article_title_name>/ // 而不是id 我已经在谷歌上搜索了几个小时,但我真的不知道如何检索文章名,虽然它仍然会显示所选文章的目标id,但url会有所不同 我不确定我是否解释得很好。u.以下是我将如何制作slug链接: 这会让你的生活变得简单一点,而不必为了找到ID而将字符串匹配到文章标题中 这是 在您的模型上有另一个属性: 在数据库表/模型中为文章创建另一列/

不确定这是否是最佳解决方案,但假设我有这样的url

?id=2
Id取自数据库,如何使其显示

/<article_title_name>/
//
而不是id

我已经在谷歌上搜索了几个小时,但我真的不知道如何检索文章名,虽然它仍然会显示所选文章的目标id,但url会有所不同

我不确定我是否解释得很好。u.

以下是我将如何制作slug链接:

这会让你的生活变得简单一点,而不必为了找到ID而将
字符串
匹配到文章标题中

这是

在您的模型上有另一个属性:
在数据库表/模型中为
文章
创建另一列/属性,称为
sluglink
或您选择的其他内容,同样有意义

创建生成它们的方法:
当您将文章保存到数据库/存储库等中时,有一个功能可以为您创建链接并将其存储在该字段中

这样,您就不必将魔术字符串反向工程到ID中;您可以在数据库/存储区等中找到匹配的slug,然后以这种方式返回它

有一个很好的功能可以做到这一点(多亏了)-在创建扩展方法时,post本身提供了方法;为了简洁起见,我刚刚对其进行了修改,并将其压缩为一个函数,以防您不确定扩展方法:

    public string GenerateSlug(string postTitle)
{
    var str = System.Text.Encoding.ASCII.GetString(System.Text.Encoding
                                        .GetEncoding("Cyrillic")
                                        .GetBytes(postTitle));

    // invalid chars           
    str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); 
    // convert multiple spaces into one space   
    str = Regex.Replace(str, @"\s+", " ").Trim(); 
    // cut and trim 
    str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim();   
    str = Regex.Replace(str, @"\s", "-"); // hyphens   
    return str;
}
publicstringgenerateslug(stringpostitle)
{
var str=System.Text.Encoding.ASCII.GetString(System.Text.Encoding
.GetEncoding(“西里尔语”)
.GetBytes(postTitle));
//无效字符
str=Regex.Replace(str,@“[^a-z0-9\s-]”,“”);
//将多个空间转换为一个空间
str=Regex.Replace(str,@“\s+”,“”)。Trim();
//修剪
str=str.Substring(0,str.Length下面是我如何进行slug链接:

这会让你的生活变得简单一点,而不必为了找到ID而将
字符串
匹配到文章标题中

这是

在您的模型上有另一个属性:
在数据库表/模型中为
文章
创建另一列/属性,称为
sluglink
或您选择的其他内容,同样有意义

创建生成它们的方法:
当您将文章保存到数据库/存储库等中时,有一个功能可以为您创建链接并将其存储在该字段中

这样,您就不必将魔术字符串反向工程到ID中;您只需在数据库/存储等中找到匹配的slug,然后以这种方式返回它

有一个很好的功能可以做到这一点(多亏了)-在创建扩展方法时,帖子本身提供了方法;为了简洁起见,我刚刚对其进行了修改,并将其压缩为一个函数,以防您不确定扩展方法:

    public string GenerateSlug(string postTitle)
{
    var str = System.Text.Encoding.ASCII.GetString(System.Text.Encoding
                                        .GetEncoding("Cyrillic")
                                        .GetBytes(postTitle));

    // invalid chars           
    str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); 
    // convert multiple spaces into one space   
    str = Regex.Replace(str, @"\s+", " ").Trim(); 
    // cut and trim 
    str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim();   
    str = Regex.Replace(str, @"\s", "-"); // hyphens   
    return str;
}
publicstringgenerateslug(stringpostitle)
{
var str=System.Text.Encoding.ASCII.GetString(System.Text.Encoding
.GetEncoding(“西里尔语”)
.GetBytes(postTitle));
//无效字符
str=Regex.Replace(str,@“[^a-z0-9\s-]”,“”);
//将多个空间转换为一个空间
str=Regex.Replace(str,@“\s+”,“”)。Trim();
//修剪

str=str.Substring(0,str.Length此链接可能会有帮助:)-适用于所有VS 2010以上版本和.NET 4.0及upI。我认为您正在寻找的是“slug”链接。使用谷歌,您将获得大量教程:)@GeoffJames谢谢你,我自己找不到它>w<'开始阅读'我已经发布了一个很好的答案,应该可以让你开始:)这个链接可能会有帮助:)-适用于所有VS 2010以上版本和.NET 4.0和upI我想你要找的是“slug”链接。有一个谷歌,你会得到大量的教程:)@GeoffJames谢谢,我自己找不到它>w<'开始阅读'我发布了一个很好的答案,应该可以让你开始阅读:)