C# 如何区分无参数和字符串参数mvc操作方法?
今天我发现很难发现两种MVC操作方法之间的区别 我的注册:C# 如何区分无参数和字符串参数mvc操作方法?,c#,asp.net-mvc,asp.net-mvc-3,routing,C#,Asp.net Mvc,Asp.net Mvc 3,Routing,今天我发现很难发现两种MVC操作方法之间的区别 我的注册: public override void RegisterArea(AreaRegistrationContext context) { // My test route. context.MapRoute( "testRoute", "Test/{action}", new { controller = "Test", action = "Index"
public override void RegisterArea(AreaRegistrationContext context)
{
// My test route.
context.MapRoute(
"testRoute",
"Test/{action}",
new { controller = "Test", action = "Index" }
);
}
以及这两种方法,这两种方法不同于所使用的http方法和参数
[HttpPost]
public ActionResult Test(TestModel model)
{
return View("Confirm", model);
}
[HttpGet]
public ActionResult Test(string title)
{
Response.Write(title);
Response.End();
return null;
}
[HttpPost]
public ActionResult Test(string title, TestModel model)
如果取消标记http方法,它将始终呈现第二个Test()方法。即使没有提供标题参数(通常由querystring/Test/Test/?title=Test提供)。可能是因为字符串是引用类型,可以为null
但如何克服这个问题呢?如何区分这些方法
提前感谢。我遵循这个签名,基本上总是使用“GET”方法签名,将模型作为最后一个参数
[HttpPost]
public ActionResult Test(TestModel model)
{
return View("Confirm", model);
}
[HttpGet]
public ActionResult Test(string title)
{
Response.Write(title);
Response.End();
return null;
}
[HttpPost]
public ActionResult Test(string title, TestModel model)
顺便说一下,我从未见过你提到的那种行为。因此,我怀疑这是否是MVC问题,而不是配置中的问题。[HttpGet]方法从不在POST方法上激发。方法是否真的是post(检查ControllerContext的请求属性)。我遵循此签名,基本上总是使用“GET”方法签名,并将模型作为最后一个参数
[HttpPost]
public ActionResult Test(TestModel model)
{
return View("Confirm", model);
}
[HttpGet]
public ActionResult Test(string title)
{
Response.Write(title);
Response.End();
return null;
}
[HttpPost]
public ActionResult Test(string title, TestModel model)
顺便说一下,我从未见过你提到的那种行为。因此,我怀疑这是否是MVC问题,而不是配置中的问题。[HttpGet]方法从不在POST方法上激发。方法真的是post(检查ControllerContext的Request属性)。好的,你说得对。谢谢没有正确地测试它!!这些http属性确实起到了作用。但是假设http属性不能使用,那么就真的不可能区分这两种方法了,不是吗?正确。如果您想通过调用
Test(null)来调用方法(例如,在unittest中),您可以看到它代码>。您必须显式地选择框,否则编译器将失败。比如Test((string)null)代码>。好吧,你说得对。谢谢没有正确地测试它!!这些http属性确实起到了作用。但是假设http属性不能使用,那么就真的不可能区分这两种方法了,不是吗?正确。如果您想通过调用Test(null)来调用方法(例如,在unittest中),您可以看到它代码>。您必须显式地选择框,否则编译器将失败。比如Test((string)null)代码>。