我们可以重载MVC控制器的操作方法吗? [英] Can we overload MVC controller action methods?

查看:101
本文介绍了我们可以重载MVC控制器的操作方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重载控制器方法或动作,我能够编写控制器动作并进行编译,但仍然没有得到结果.

I was trying to overload controller method or action I am able to write controller action and compile but still I am not getting result.

   public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
    public ActionResult Contact(string str)
    {
        ViewBag.Message = "Your contact page by overloaded method";

        return View();
    }

但是我仍然在关注错误 "/"应用程序中的服务器错误

but still I am getting following Error Server Error in '/' Application

推荐答案

之所以会发生冲突,是因为string可为空.

You get the conflict because because string is nullable.

您可以使用[ActionName("NewActionName")]保持原样的方法名称来提供其他操作名称.

You can use [ActionName("NewActionName")] to give a different action name by leaving the method name intact.

如果您有其他类似的操作方法,则不要冲突

You shouldn't get conflict if you have another action method like this

public ActionResult Contact(int Value)

但是,下面的操作将再次失败

But, below action will again fail

public ActionResult Contact(int? Value)

原因很明显.该参数再次可以为空.

The reason is obvious. The parameter is again nullable.

这篇关于我们可以重载MVC控制器的操作方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆