C#学习教程:在ASP.NETMVC5中路由可选参数我们使用属性Route来映射Web应用程序中的路由。我有以下操作:[Route("{type}/{library}/{version}/{file?}/{renew?}")]publicActionResultIndex(EFileTypetype,stringlibrary,stringversion,stringfile=null,ECacheTyperenew=ECacheType.cache){//code...}如果我们在url的末尾传递一个斜杠字符/,我们只能访问这个URL,如下所示:type/lib/version/file/cache/it工作正常,但不工作/不工作,我收到404notfound错误,比如这个type/lib/version/file/cache或这个(没有可选参数):type/lib/versionIwanttouseornot在url的末尾使用/char访问。我的最后两个参数是可选的。我的RouteConfig.cs是这样的:路线.MapMvcAttributeRoutes();}}我应该怎么解决?斜杠/也是可选的?也许您应该尝试将枚举更改为整数?我就是这样做的{file?}/{renew?}")]publicActionResultIndex2(EFileTypetype,stringlibrary,stringversion,stringfile=null,ECacheTyperenew=ECacheType.cache){returnView("Index");}}我publicstaticvoidRegisterRoutes(RouteCollectionroutes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");//在控制器中启用路由属性routes.MapMvcAttributeRoutes();routes.MapRoute(name:"Default",url:"{controller}/{action}/{id}",defaults:new{controller="Home",action="Index",id=UrlParameter.Optional});然后我可以输入Calledhttp://localhost:52392/2/lib1/ver1/file1/1http://localhost:52392/2/lib1/ver1/file1http://localhost:52392/2/lib1/ver1或http://localhost:52392/2/lib1/ver1/file1/1/http://localhost:52392/2/lib1/ver1/file1/http://localhost:52392/2/lib1/ver1/可以正常使用...以上是C#的学习tutorial:ASP.NETMVC5中路由可选参数分享的所有内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注—//itsworkingwithmvc5[Route("Projects/{Id}/{Title}")]publicActionResultIndex(longId,stringTitle){returnview();}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
