Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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# Asp.NETMVC5路由未命中_C#_Asp.net_.net_Asp.net Mvc_Iis Express - Fatal编程技术网

C# Asp.NETMVC5路由未命中

C# Asp.NETMVC5路由未命中,c#,asp.net,.net,asp.net-mvc,iis-express,C#,Asp.net,.net,Asp.net Mvc,Iis Express,我的路线图中有: routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute(

我的路线图中有:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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


routes.MapRoute(
    name: "StoreSearch",
    url: "{storeName}",
    defaults: new { controller = "Search", action = "Store" }
);
基本上我有第二条路线,所以如果我输入
localhost:9000/facebook
没有名为
facebook
的控制器,那么我的第二条路线应该选择
facebook
作为
storeName
并点击我的
Search
控制器的
Store
操作。但现在我得到了404


有什么办法可以解决这个问题吗?

如果您有特定的存储集或验证它们的方法,您可以向路由添加约束

使用匹配谓词添加泛型约束的步骤

public class ServerRouteConstraint : IRouteConstraint
{
    private readonly Func<Uri, bool> _predicate;

    public ServerRouteConstraint(Func<Uri, bool> predicate)
    {
        this._predicate = predicate;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName,
        RouteValueDictionary values, RouteDirection routeDirection)
    {
        return this._predicate(httpContext.Request.Url);
    }
}
例如,请检查:


此外,应将路线从最特定添加到最一般。

您应将任何自定义路线放置在
默认路线上方

只要交换订单,你就可以走了

//before default route
routes.MapRoute(
    name: "StoreSearch",
    url: "{storeName}",
    defaults: new { controller = "Search", action = "Store" }
);

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

路线冲突,因为这些路线太一般。第一条路线将与任何试图进入第二条路线的尝试相匹配。谢谢。作为实现我需要的替代方案,你有什么建议?你希望执行哪个控制器和操作?基本上,它会正常工作。例如,如果我有家庭控制器,它应该转到家庭/操作-非常标准的东西,但如果我输入沃尔玛,facebook等我没有它的控制器在我的项目中,所以第一条路线不应该被击中它应该把沃尔玛作为店名参数,并调用我的搜索控制器的商店行动。但是如果我有Facebook的控制器,那么很明显,它会受到攻击。如果我不清楚,请告诉我。因此,您想知道何时调用搜索控制器的存储操作。用户执行
{host}/facebook
{host}/walmart
,这并不容易。我已经试过了。但是,如果我有一个家庭控制器,那么我的搜索/存储也会被触发。我希望如果控制器存在,则使用该控制器,否则使用我的搜索/存储操作。
//before default route
routes.MapRoute(
    name: "StoreSearch",
    url: "{storeName}",
    defaults: new { controller = "Search", action = "Store" }
);

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