public class HomeController : Controller
{
public ActionResult Index(int para1,string para2,int para3,bool para4,CustomClass para5)
{
return View();
}
}
public class CustomClass
{
}
如上代码,如果想使用Home/Index路径路由到Index方法
方式1:从参数动手,设置可为null的参数比如int? para1,或者给参数默认值比如int para1 = 0,或者把简单参数合为一个复杂类型,这样就可以默认为null了。
方式2:从路由动手
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index", para1 = 0, para2 = 0, para3 = 0, para4 = 0 }
);
现在想问的是,有没有第三种方式,比如说配置一下哪里,或者有比较简约的解决方案。不用动最上面代码的参数,也不用为每一个想如此做的Action都单独配置路由,所有参数在请求时不传输的话均使用默认值。让Home/Index直接到Aciton方法。
虽然这样做的话可能会丧失一些特性(参数重载的Action什么的),但是还是想问一下
6 个解决方案
#1
public class HomeController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var methodInfo = ((ReflectedActionDescriptor) filterContext.ActionDescriptor).MethodInfo;
foreach (var p in methodInfo.GetParameters())
{
if (p.ParameterType.IsValueType)
{
filterContext.ActionParameters[p.Name] = Activator.CreateInstance(p.ParameterType);
}
}
}
public ActionResult Index(int para1, string para2, int para3, bool para4)
{
Debug.Print("para1:{0} para2:{1} para3:{2} para4 :{3}",para1, para2, para3, para4);
return View();
}
}
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var methodInfo = ((ReflectedActionDescriptor) filterContext.ActionDescriptor).MethodInfo;
foreach (var p in methodInfo.GetParameters())
{
if (p.ParameterType.IsValueType)
{
filterContext.ActionParameters[p.Name] = Activator.CreateInstance(p.ParameterType);
}
}
}
public ActionResult Index(int para1, string para2, int para3, bool para4)
{
Debug.Print("para1:{0} para2:{1} para3:{2} para4 :{3}",para1, para2, para3, para4);
return View();
}
}
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
#2
asp.net mvc 5可以有第三种方式
google attribute routing
google attribute routing
#3
#4
虽然乍一看觉得不太可行(因为如果路由不到这个action的话,加filter应该无用吧),但是我还是先去试一试哈
#5
看过几篇文章,不过没印象那个可以简单的一次配置得到效果啊。
等下去看看
#6
可用!~
看来是我理解错啦!
#1
public class HomeController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var methodInfo = ((ReflectedActionDescriptor) filterContext.ActionDescriptor).MethodInfo;
foreach (var p in methodInfo.GetParameters())
{
if (p.ParameterType.IsValueType)
{
filterContext.ActionParameters[p.Name] = Activator.CreateInstance(p.ParameterType);
}
}
}
public ActionResult Index(int para1, string para2, int para3, bool para4)
{
Debug.Print("para1:{0} para2:{1} para3:{2} para4 :{3}",para1, para2, para3, para4);
return View();
}
}
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var methodInfo = ((ReflectedActionDescriptor) filterContext.ActionDescriptor).MethodInfo;
foreach (var p in methodInfo.GetParameters())
{
if (p.ParameterType.IsValueType)
{
filterContext.ActionParameters[p.Name] = Activator.CreateInstance(p.ParameterType);
}
}
}
public ActionResult Index(int para1, string para2, int para3, bool para4)
{
Debug.Print("para1:{0} para2:{1} para3:{2} para4 :{3}",para1, para2, para3, para4);
return View();
}
}
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
#2
asp.net mvc 5可以有第三种方式
google attribute routing
google attribute routing
#3
#4
public class HomeController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var methodInfo = ((ReflectedActionDescriptor) filterContext.ActionDescriptor).MethodInfo;
foreach (var p in methodInfo.GetParameters())
{
if (p.ParameterType.IsValueType)
{
filterContext.ActionParameters[p.Name] = Activator.CreateInstance(p.ParameterType);
}
}
}
public ActionResult Index(int para1, string para2, int para3, bool para4)
{
Debug.Print("para1:{0} para2:{1} para3:{2} para4 :{3}",para1, para2, para3, para4);
return View();
}
}
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
虽然乍一看觉得不太可行(因为如果路由不到这个action的话,加filter应该无用吧),但是我还是先去试一试哈
#5
asp.net mvc 5可以有第三种方式
google attribute routing
看过几篇文章,不过没印象那个可以简单的一次配置得到效果啊。
等下去看看
#6
public class HomeController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var methodInfo = ((ReflectedActionDescriptor) filterContext.ActionDescriptor).MethodInfo;
foreach (var p in methodInfo.GetParameters())
{
if (p.ParameterType.IsValueType)
{
filterContext.ActionParameters[p.Name] = Activator.CreateInstance(p.ParameterType);
}
}
}
public ActionResult Index(int para1, string para2, int para3, bool para4)
{
Debug.Print("para1:{0} para2:{1} para3:{2} para4 :{3}",para1, para2, para3, para4);
return View();
}
}
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
虽然乍一看觉得不太可行(因为如果路由不到这个action的话,加filter应该无用吧),但是我还是先去试一试哈
可用!~
看来是我理解错啦!