Asp.net mvc Url中没有操作名称的路由

Asp.net mvc Url中没有操作名称的路由,asp.net-mvc,asp.net-mvc-routing,action,Asp.net Mvc,Asp.net Mvc Routing,Action,我的路线有问题。我有新闻控制器,可以从urlhttp://localhost/News/Details/1/news-title(slug)。现在没有问题。但我创建了一个名为Services的控制器,它具有索引操作。路由配置: routes.MapRoute( "Services", "Services/{id}", new { controller = "Services", action = "Index" } ); 当我的索引操作 public ActionRe

我的路线有问题。我有新闻控制器,可以从url
http://localhost/News/Details/1/news-title(slug)
。现在没有问题。但我创建了一个名为Services的控制器,它具有索引操作。路由配置:

routes.MapRoute(
    "Services",
    "Services/{id}",
    new { controller = "Services", action = "Index" }
); 
当我的索引操作

public ActionResult Index(string title)
{
    return View(title);
}
我在浏览器中手工编写
localhost:5454/Services/sometext
,它可以正常工作

但是当我的索引操作更改为

public ActionResult Index(string title)
{
    Service service = _myService.Find(title);
    ServiceViewModel = Mapper.Map<Service , ServiceViewModel >(service );
    if (service == null)
    {
        return HttpNotFound();
    }
    return View(serviceViewModel);
}
公共操作结果索引(字符串标题)
{
服务服务=_myService.Find(title);
ServiceViewModel=Mapper.Map(服务);
if(服务==null)
{
返回HttpNotFound();
}
返回视图(serviceViewModel);
}
我发现Url localhost/Services/ITServices的Http错误404未找到

我可以从管理页面添加这个服务,标题是(例如ITServices)。 然后我在我的主页上为服务链接做foreach

@foreach (var service Model.Services)
{
    <div class="btn btn-success btn-sm btn-block">
     @Html.ActionLink(service.Title, "Index", new { controller = "Services", id = service.Title }) 
    </div>
}
@foreach(var-service-Model.Services)
{
@ActionLink(service.Title,“Index”,new{controller=“Services”,id=service.Title})
}
但是我无法在
localhost/Services/ITServices
中显示该页面

当我点击这个链接时,我想转到localhost/Services/ITServices页面,它必须显示内容(可以从admin页面添加),就像在新闻中一样。但我不想像新闻中那样将其与动作名和ID一起使用。我怎样才能做到这一点

编辑

嗯。我将
findbytle(字符串标题)
添加到存储库中。我在RouteConfig和主页视图中的链接中将
id
更改为
title
。然后在我的域模型中删除Id并将标题更新为[Key]。现在它起作用了。从管理员页面添加新内容时,只需通过远程验证检查标题是否可能重复。

尝试以下操作:

public ActionResult Index(string id)
试试这个:

public ActionResult Index(string id)

URL模板中的参数名称与操作(
索引
)上的参数不匹配

所以你可以做两件事中的一件

更改模板参数以匹配操作的参数

routes.MapRoute(
   name: "Services",
   url: "Services/{title}",
   defaults: new { controller = "Services", action = "Index" }
); 
动作索引在哪里

public ActionResult Index(string title) { ... }
或者更改操作索引的参数以匹配url模板中的参数

public ActionResult Index(string id) { ... }
路线图在哪里

routes.MapRoute(
   name: "Services",
   url: "Services/{id}",
   defaults: new { controller = "Services", action = "Index" }
); 
但无论如何,路由都找不到路由,因为它无法匹配参数

事实上,如果你的目的是把这个标题当作一个鼻涕虫,那么你甚至可以使用一个包罗万象的服务路线,比如

routes.MapRoute(
   name: "Services",
   url: "Services/{*title}",
   defaults: new { controller = "Services", action = "Index" }
); 
看看这里提供的答案


URL模板中的参数名称与操作(
索引
)上的参数不匹配

所以你可以做两件事中的一件

更改模板参数以匹配操作的参数

routes.MapRoute(
   name: "Services",
   url: "Services/{title}",
   defaults: new { controller = "Services", action = "Index" }
); 
动作索引在哪里

public ActionResult Index(string title) { ... }
或者更改操作索引的参数以匹配url模板中的参数

public ActionResult Index(string id) { ... }
路线图在哪里

routes.MapRoute(
   name: "Services",
   url: "Services/{id}",
   defaults: new { controller = "Services", action = "Index" }
); 
但无论如何,路由都找不到路由,因为它无法匹配参数

事实上,如果你的目的是把这个标题当作一个鼻涕虫,那么你甚至可以使用一个包罗万象的服务路线,比如

routes.MapRoute(
   name: "Services",
   url: "Services/{*title}",
   defaults: new { controller = "Services", action = "Index" }
); 
看看这里提供的答案