Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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
Asp.net mvc ASP.NET MVC 4路由查询-将查询字符串传递给索引操作_Asp.net Mvc_Asp.net Mvc Routing - Fatal编程技术网

Asp.net mvc ASP.NET MVC 4路由查询-将查询字符串传递给索引操作

Asp.net mvc ASP.NET MVC 4路由查询-将查询字符串传递给索引操作,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,我有一个带有索引操作的控制器 public ActionResult Index(int id = 0) { return view(); } 我希望将id传递给index操作,但是它的工作方式似乎与details操作不同 e、 g.如果我想将id 4传递到索引操作中,我必须访问url: http://localhost:8765/ControllerName/?id=4 随着行动的细节。。。我能做到 http://localhost:8765/ControllerName/Det

我有一个带有索引操作的控制器

public ActionResult Index(int id = 0)
{

    return view();
}
我希望将id传递给index操作,但是它的工作方式似乎与details操作不同

e、 g.如果我想将id 4传递到索引操作中,我必须访问url:

http://localhost:8765/ControllerName/?id=4
随着行动的细节。。。我能做到

http://localhost:8765/ControllerName/Details/4
我想用索引做的是

http://localhost:8765/ControllerName/4
当我访问此url时,我收到一个错误:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /fix/1

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
这可能吗?如何让MVC以与细节操作相同的方式自动处理索引操作

谢谢

更新-我的当前路线配置

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
当我访问localhost:1234/Fix/3时,更新新的RouteConfig类仍然无法工作

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        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: "FixIndexWithParam",
            url: "Fix/{id}",
            defaults: new { controller = "Fix", action = "Index", id = UrlParameter.Optional });
    }
}

更新值得指出的是,/ControllerName/Index/4应该与默认路由一起工作

在默认路由存在的情况下,它希望第二个参数是控制器名称

因此,默认路由/ControllerName/4被解释为
ControllerNameController
Action
4
,这当然不存在

如果你加上

routes.MapRoute(
name: "IndexWithParam",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
在默认值之前,它将允许

/Home/4要路由到
Home控制器
操作
索引
id=4

我还没有测试这个,它可能与默认值冲突。您可能需要在路由中明确指定控制器,即:

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

(显然,将
Home
替换为您实际想要路由到的任何控制器)

我们需要查看您的
routeConfig.cs
fileQuestion中的内容,并用我的RouteConfig更新问题Yi:我编辑了您的问题以删除对EntityFramework的引用,此问题与EF无关,这都是关于.NET路由实现以及它将这些请求映射到MVC控制器的方式谢谢,我创建了一个新路由,但仍然存在问题。。。请参阅更新的问题。抱歉,我在粘贴前已回答。请检查更新您需要在默认路由之前添加新路由,它们按顺序执行您还可以添加路由约束,以便仅当参数为整数时它才会匹配。如何附加路由约束?