简体   繁体   English

无法找到Asp.net MVC 4 Url Resource

[英]Asp.net MVC 4 Url Resource cannot be found

In my Index page I am generating url using the following 在我的索引页面中,我使用以下内容生成URL

@foreach(var c in ViewBag.cities)
{
   <li>@Html.ActionLink((string)c.CityName,"somepage",new{city=c.CityName, id=c.Id})</li>
}

Which generated urls for every city in the following format 以下列格式为每个城市生成了网址

localhost:55055/1/city1
localhost:55055/2/city2
...

where 1,2 are Id and city1, city2 are CityName 其中1,2是Id和city1,city2是CityName

I have configured the route as 我已将路线配置为

routes.MapRoute(
            name: "CityRoute",
            url: "{id}/{city}",
            defaults: new { controller = "Home", action = "somepage", id = UrlParameter.Optional, city = UrlParameter.Optional }
            );

The somepage action method in Home Controller: Home Controller中的somepage动作方法:

public string somepage(int id, string city)
{
  return city + " " + id;
}

Even I tried this 即使我试过这个

public string somepage()
{
   return "Hello";
}

but it results in Resource cannot be found 但它导致Resource cannot be found

I tried putting breakpoint in sompage which is never hit. 我尝试将断点放在从未被击中的sompage中。

Any suggesstions 任何建议

Update 更新

As pointed out by @KD in comments below I changed the routes order and placed above mentioned rule above all rules. 正如@KD在下面的评论中指出的那样,我改变了路线顺序并将上述规则置于所有规则之上。 I also changed the method 我也改变了方法

 public ActionResult somepage(int id=0, string city="")
 {
    return Content(city + " " + id);
 }

Now the behavior changed and the default rule 现在行为已更改并且默认规则

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

is not used for normal index page. 不用于普通索引页面。 Instead this use is used since the breakpoint in method somepage is hit and If I put http://localhost:55055/2/cityname in the browser, the page does display id and cityname. 而是使用此用法,因为方法somepage的断点被命中,如果我在浏览器中放入http://localhost:55055/2/cityname ,页面会显示id和cityname。 But now default route is not used for application home http://localhost:55055/ 但现在默认路由不用于应用程序主页http://localhost:55055/

The pattern of your new route and pattern of default route is almost same hence it will always create conflict to match those routes .. change your route to following 您的新路线和默认路线模式的模式几乎相同,因此它将始终产生冲突以匹配这些路线。将您的路线更改为以下

routes.MapRoute(
            name: "CityRoute",
            url: "CitySearch/{id}/{city}",
            defaults: new { controller = "Home", action = "somepage", id = UrlParameter.Optional, city = UrlParameter.Optional }
            );

ie add some prefix to your pattern so it can get easily identified.. like "CitySearch" and then mention your route name while providing Action link for it.. 即为您的模式添加一些前缀,以便它可以轻松识别..像“CitySearch”,然后提供您的路线名称,同时为它提供行动链接..

Or if you don't want to add prefix to it then do the following thing and it will work like a charm.. 或者,如果您不想为它添加前缀,那么请执行以下操作,它将像魅力一样工作。

For your CityRoute add a Route Constraint for Id which will check if the ID field is Integer. 对于您的CityRoute,为Id添加一个Route Constraint,它将检查ID字段是否为Integer。 for normal URLS it will return false and hence your default route will get evaluated ... try this... 对于普通的URL,它将返回false,因此您的默认路由将被评估...试试这个......

routes.MapRoute(
            name: "CityRoute",
            url: "{id}/{city}",
            defaults: new { controller = "Home", action = "somepage", id = UrlParameter.Optional, city = UrlParameter.Optional },
            constraints: new {id = @"\d+"}
            );

This is the Regular Expression Constraint.. Put this route at the top and check. 这是正则表达式约束..将此路线放在顶部并检查。

As you have not defined Action it won't be called thus error is coming 由于您尚未定义Action,因此不会调用它,因此会出现错误

Try this, 尝试这个,

public ActionResult somepage(int id, string city)
    {
        return Content(city + " " + id);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM