I am creating an ASP.NET MVC 5 application and I have some issues with routing. We are using the attribute Route
to map our routes in the web application. I have the following action:
我正在创建一个ASP.NET MVC 5应用程序,我遇到了一些路由问题。我们使用属性Route来映射Web应用程序中的路由。我有以下行动:
[Route("{type}/{library}/{version}/{file?}/{renew?}")]
public ActionResult Index(EFileType type,
string library,
string version,
string file = null,
ECacheType renew = ECacheType.cache)
{
// code...
}
We only can access this URL if we pass the slash char /
in the end of url
, like this:
如果我们在url的末尾传递斜杠char /,我们只能访问此URL,如下所示:
type/lib/version/file/cache/
It works fine but does not work without /
, I get a 404
not found error, like this
它工作正常,但没有/没有工作,我得到404未找到错误,像这样
type/lib/version/file/cache
or this (without optional parameters):
或者这个(没有可选参数):
type/lib/version
I would like to access with or without /
char at the end of url
. My two last parameters are optional.
我想在url的末尾使用或不使用/ char访问。我的最后两个参数是可选的。
My RouteConfig.cs
is like this:
我的RouteConfig.cs是这样的:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
}
How can I solve it? Make the slash /
be optional too?
我该如何解决?斜杠/也是可选的?
2 个解决方案
#1
23
Maybe you should try to have your enums as integers instead?
也许你应该尝试将你的枚举改为整数?
This is how I did it
这就是我做到的
public enum ECacheType
{
cache=1, none=2
}
public enum EFileType
{
t1=1, t2=2
}
public class TestController
{
[Route("{type}/{library}/{version}/{file?}/{renew?}")]
public ActionResult Index2(EFileType type,
string library,
string version,
string file = null,
ECacheType renew = ECacheType.cache)
{
return View("Index");
}
}
And my routing file
和我的路由文件
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// To enable route attribute in controllers
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
I can then make calls like
然后我就可以打电话了
http://localhost:52392/2/lib1/ver1/file1/1
http://localhost:52392/2/lib1/ver1/file1
http://localhost:52392/2/lib1/ver1
or
要么
http://localhost:52392/2/lib1/ver1/file1/1/
http://localhost:52392/2/lib1/ver1/file1/
http://localhost:52392/2/lib1/ver1/
and it works fine...
它工作得很好......
#2
-1
//its working with mvc5
[Route("Projects/{Id}/{Title}")]
public ActionResult Index(long Id, string Title)
{
return view();
}
#1
23
Maybe you should try to have your enums as integers instead?
也许你应该尝试将你的枚举改为整数?
This is how I did it
这就是我做到的
public enum ECacheType
{
cache=1, none=2
}
public enum EFileType
{
t1=1, t2=2
}
public class TestController
{
[Route("{type}/{library}/{version}/{file?}/{renew?}")]
public ActionResult Index2(EFileType type,
string library,
string version,
string file = null,
ECacheType renew = ECacheType.cache)
{
return View("Index");
}
}
And my routing file
和我的路由文件
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// To enable route attribute in controllers
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
I can then make calls like
然后我就可以打电话了
http://localhost:52392/2/lib1/ver1/file1/1
http://localhost:52392/2/lib1/ver1/file1
http://localhost:52392/2/lib1/ver1
or
要么
http://localhost:52392/2/lib1/ver1/file1/1/
http://localhost:52392/2/lib1/ver1/file1/
http://localhost:52392/2/lib1/ver1/
and it works fine...
它工作得很好......
#2
-1
//its working with mvc5
[Route("Projects/{Id}/{Title}")]
public ActionResult Index(long Id, string Title)
{
return view();
}