ASP.NET Core MVC 1.0多个post方法在一个Controller中

时间:2016-04-29 05:27:27

标签: asp.net-core asp.net-core-mvc dnx

我可以使用以下代码执行此操作:

04-29 10:47:50.870 20286-20286/com.demo.a_fatal E/Trace: error opening trace file: No such file or directory (2)
04-29 10:47:50.910 20286-20286/com.demo.a_fatal E/InstantRun: Could not find slices in APK; aborting.
04-29 10:47:50.920 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.app.Application$OnProvideAssistDataListener', referenced from method com.demo.a_fatal.applications.AFatalApp.access$super
04-29 10:47:50.920 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.app.Application$OnProvideAssistDataListener', referenced from method com.demo.a_fatal.applications.AFatalApp.access$super
04-29 10:47:51.010 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
04-29 10:47:51.010 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
04-29 10:47:51.080 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.080 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.080 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.080 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.090 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.100 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.100 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.100 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.transition.TransitionManager', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.100 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.110 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.110 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.110 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.demo.a_fatal.SplashActivity.access$super
04-29 10:47:51.130 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
04-29 10:47:51.130 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
04-29 10:47:51.130 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.media.session.MediaController', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
04-29 10:47:51.130 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.widget.Toolbar', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
04-29 10:47:51.130 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
04-29 10:47:51.130 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
04-29 10:47:51.130 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
04-29 10:47:51.130 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.transition.TransitionManager', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
04-29 10:47:51.130 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
 04-29 10:47:51.140 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
 04-29 10:47:51.150 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.demo.a_fatal.global.AFatalActivity.access$super
04-29 10:47:51.150 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method  com.demo.a_fatal.global.AFatalActivity.access$super
04-29 10:47:51.160 20286-20286/com.demo.a_fatal E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method com.demo.a_fatal.dialog.ProgressDialog.access$super

但我不能这样做:

    [HttpPost("SampleRoute1")]
    public JsonResult Post([FromBody]SampleModel1 value)
    {
        .....Functionone.....
        return Json("");
    }

    [HttpPost("SampleRoute2")]
    public JsonResult Post([FromBody]SampleModel2 value)
    {
        .....Functiontwo.....
        return Json("");
    }

它会出错" 键入' Controller1'已经定义了一个名为' Post'使用相同的参数类型"

那么有什么办法可以让一个控制器中的两个Post具有相同的参数但具有不同的路径?

像这样:

发布(SampleModel1)=> "控制器1 \ SampleRoute1" =>做功能1

发布(SampleModel1)=> "控制器1 \ SampleRoute2" =>做功能2

2 个答案:

答案 0 :(得分:8)

是的,你可以这样做。问题是你正在尝试在一个具有相同名称和类别的类中使用两种方法。参数和那是不可能的。您应该将方法的名称更改为不同的名称。

请注意动作名称&已在HttpPost属性中指定了帖子请求类型,因此您不必依赖方法名称。

[HttpPost("SampleRoute1")]
public JsonResult Aaa([FromBody]SampleModel1 value)
{
    .....Functionone.....
    return Json("");
}

[HttpPost("SampleRoute2")]
public JsonResult Bbb([FromBody]SampleModel1 value)
{
    .....Functiontwo.....
    return Json("");
}

答案 1 :(得分:3)

您收到错误是因为您有两个相同的方法。你怎么知道要执行哪一个?您是基于您定义的路线吗?

如果我给你吃了两个相同的红苹果,那两个苹果没有区别,我告诉你吃正确的苹果,你知道哪个是正确的苹果吗?

您将不得不更改您的方法名称,以便它们是唯一且可识别的。

[HttpPost("SampleRoute1")]
public ActionResult Function1(SampleModel1 model)
{
     return Json("");
}

[HttpPost("SampleRoute2")]
public ActionResult Function2(SampleModel1 model)
{
     return Json("");
}

基于以上所述,将发生以下情况:

  • 现在发布SampleModel1时,使用路线Controller1\SampleRoute1将执行操作方法Function1
  • 现在发布SampleModel2时,使用路线Controller1\SampleRoute2将执行操作方法Function2