且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

解决歧义

更新时间:2023-11-30 22:24:34

默认情况下,重载方法在ASP.NET MVC中不被支持。你必须使用不同的行动或可选参数。例如:

By default, overloading methods is not supported in ASP.NET MVC. You have to use difference actions or optional parameters. For example:

public ActionResult Create() {}
public ActionResult Create(string Skill, int ProductId) {}
public ActionResult Create(Skill Skill, Component Comp) {}

将变为:

// [HttpGet] by default
public ActionResult Create() {}

[HttpPost]
public ActionResult Create(Skill skill, Component comp, string strSkill, int? productId) {
    if(skill == null && comp == null 
        && !string.IsNullOrWhiteSpace(strSkill) && productId.HasValue)
        // do something...
    else if(skill != null && comp != null
        && string.IsNullOrWhiteSpace(strSkill) && !productId.HasValue)
        // do something else
    else
        // do the default action
}

// [HttpGet] by default
public ActionResult Create() {}

[HttpPost]
public ActionResult Create(string Skill, int ProductId) {}

[HttpPost]
public ActionResult CreateAnother(Skill Skill, Component Comp) {}

public ActionResult Create() {}
[ActionName("CreateById")]
public ActionResult Create(string Skill, int ProductId) {}
[ActionName("CreateByObj")]
public ActionResult Create(Skill Skill, Component Comp) {}

又见这个Q&安培; A