Asp.net mvc 使用连字符参数的MVC高级路由

Asp.net mvc 使用连字符参数的MVC高级路由,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc Routing,我在MVC5网站上有一个新闻和一个汽车部分,到目前为止,我已经定义了以下新闻路线 //route to cheat routing engine to generate id-slug routes.MapRoute("NewsForReal", "news/{idandslug}", new { controller = "News", action = "Show" }); routes.MapRoute("News", "news/{id}-{slug}", new { Controlle

我在MVC5网站上有一个新闻和一个汽车部分,到目前为止,我已经定义了以下新闻路线

//route to cheat routing engine to generate id-slug
routes.MapRoute("NewsForReal", "news/{idandslug}", new { controller = "News", action = "Show" });
routes.MapRoute("News", "news/{id}-{slug}", new { Controller = "News", action = "Show" });
这是因为my
NewsController.cs
文件中的以下代码将上述路由的参数拆分/连接在一起

public ActionResult Show(string idandslug)
    {
        var parts = SeperateIDandSlug(idandslug);
        if (parts == null)
            return HttpNotFound();

        var news = Database.Session.Load<News>(parts.Item1);
        if (news == null || news.IsDeleted)
            return HttpNotFound();

        //redirect urls using correct slug if incorrect - SEO broken URLs
        if (!news.Slug.Equals(parts.Item2, StringComparison.CurrentCultureIgnoreCase))
            return RedirectToRoutePermanent("Post", new { id = parts.Item1, slug = news.Slug });

        return View(new NewsShow
        {
            News = news
        });

    }

    private Tuple<int, string> SeperateIDandSlug(string idandslug)
    {
        var matches = Regex.Match(idandslug, @"^(\d+)\-(.*)?$");
        if (!matches.Success)
            return null;

        var id = int.Parse(matches.Result("$1"));
        var slug = matches.Result("$2");
        return Tuple.Create(id, slug);
    }
公共操作结果显示(字符串idandslug)
{
var部分=单独的IDA和SLUG(IDA和SLUG);
if(parts==null)
返回HttpNotFound();
var news=Database.Session.Load(parts.Item1);
if(news==null | | news.IsDeleted)
返回HttpNotFound();
//如果不正确,使用正确的slug重定向URL-搜索引擎优化破坏的URL
如果(!news.Slug.Equals(parts.Item2,StringComparison.CurrentCultureIgnoreCase))
返回RedirectToRoutePermanent(“Post”,new{id=parts.Item1,slug=news.slug});
返回视图(新新闻节目)
{
新闻
});
}
私有元组分隔idandslug(字符串idandslug)
{
var matches=Regex.Match(idandslug,@“^(\d+)\-(.*)”;
如果(!matches.Success)
返回null;
var id=int.Parse(匹配.Result($1));
var slug=匹配。结果($2”);
返回Tuple.Create(id,slug);
}
我还希望对cars部分使用类似的格式,其中第一个值始终是实体ID:

“/carsforsale/15489-ford-fiesta-2009-petrol-manual-black”

我可以通过复制和更新我的
separateIDandslug
函数,然后添加构建URL所需的更多参数来实现这一点吗?或者我可以通过使用约束来完成工作吗

提前感谢您的帮助

routes.MapRoute("Cars", "cars/{id}-{make}-{model}-{year}-{fueltype}-{transmission}-{colour}", new { controller = "Cars", action = "Detail" }, new { id = @"(\d+)" }); 

routes.MapRoute("News", "news/{id}-{slug}", new { controller = "News", action = "Show" }, new { id = @"(\d+)" });

以上代码为我工作

好的,我想我已经通过使用约束和正则表达式破解了它,但是如果有人能确认这将起作用,并且如果我能用相同的方法替换我以前的新闻路线,我将非常感激<代码>routes.MapRoute(“汽车”,“汽车/{id}-{make}-{model}-{year}-{fueltype}-{transmission}-{color}”,新的{controller=“Cars”,action=“Detail”},新的{id=@”(\\d+)
routes.MapRoute(“新闻”,“新闻/{id}-{slug}”,新{controller=“新闻”,action=“Show”},新{id=@”(\\d+)”})