如何将接口参数传递给 RouteValueDictionary()?
How to pass interface parameter to RouteValueDictionary()?
在 ASP.NET MVC 应用程序中,我将接口实例作为参数传递。在下面的代码片段中,myinterface 是接口实例。
return RedirectToAction( "Main", new RouteValueDictionary(
new { controller = controllerName, action = "Main", Id = Id, someInterface = myinterface } ) );
在接收方,操作如下所示:
public ActionResult Index(Int Id, ISomeInterface someInterface) {...}
我得到以下运行时异常:
Cannot create an instance of an interface
有什么办法吗?
不知道你的理由是什么。我假设它们是有效的。 MVC 不会为您的接口提供实现。您必须像下面那样覆盖默认模型绑定行为并提供具体类型(它可以来自您的 IOC 容器):
public class MyBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext
, ModelBindingContext bindingContext, Type modelType)
{
if (bindingContext.ModelType.Name == "ISomeInterface")
return new SomeType();
//You can get the concrete implementation from your IOC container
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}
public interface ISomeInterface
{
string Val { get; set; }
}
public class SomeType : ISomeInterface
{
public string Val { get; set; }
}
然后在您的“应用程序开始”中将如下所示:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new MyBinder();
//Followed by other stuff
}
}
这是工作操作
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
var routeValueDictionary = new RouteValueDictionary()
{
{"id",1},
{"Val","test"}
};
return RedirectToAction("Abouts", "Home", routeValueDictionary);
}
public ActionResult Abouts(int id, ISomeInterface testInterface)
{
ViewBag.Message = "Your application description page.";
return View();
}
在 ASP.NET MVC 应用程序中,我将接口实例作为参数传递。在下面的代码片段中,myinterface 是接口实例。
return RedirectToAction( "Main", new RouteValueDictionary(
new { controller = controllerName, action = "Main", Id = Id, someInterface = myinterface } ) );
在接收方,操作如下所示:
public ActionResult Index(Int Id, ISomeInterface someInterface) {...}
我得到以下运行时异常:
Cannot create an instance of an interface
有什么办法吗?
不知道你的理由是什么。我假设它们是有效的。 MVC 不会为您的接口提供实现。您必须像下面那样覆盖默认模型绑定行为并提供具体类型(它可以来自您的 IOC 容器):
public class MyBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext
, ModelBindingContext bindingContext, Type modelType)
{
if (bindingContext.ModelType.Name == "ISomeInterface")
return new SomeType();
//You can get the concrete implementation from your IOC container
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}
public interface ISomeInterface
{
string Val { get; set; }
}
public class SomeType : ISomeInterface
{
public string Val { get; set; }
}
然后在您的“应用程序开始”中将如下所示:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new MyBinder();
//Followed by other stuff
}
}
这是工作操作
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
var routeValueDictionary = new RouteValueDictionary()
{
{"id",1},
{"Val","test"}
};
return RedirectToAction("Abouts", "Home", routeValueDictionary);
}
public ActionResult Abouts(int id, ISomeInterface testInterface)
{
ViewBag.Message = "Your application description page.";
return View();
}