如何在ASP.NET MVC 3中重载索引ActionResult [英] How to overload the Index ActionResult in ASP.NET MVC 3

查看:63
本文介绍了如何在ASP.NET MVC 3中重载索引ActionResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

我想重载ASP.NET MVC3中的默认Index ActionResult方法,例如:

Hi mates,

I want to overload the default Index ActionResult method in ASP.NET MVC3 like:

public ActionResult Index()
       {
           ViewBag.Message = "Hello from Parameter-Less Index Method.";
           return View();
       }


       public ActionResult Index(string myName)
       {
           ViewBag.Message = "Hello from Parameterized Index Method. <br/>Hello "+myName;
           return View();

       }



可能吗?如果是,该怎么办?感谢您的帮助!

最好的问候,
Sunny K



is it possible? If yes, the how? I appreciate any help !

Best regards,
Sunny K

推荐答案

我认为索引方法不允许重载.

临时方法是:

I guess overloading in not allowed for index method.

The temporary way is:

public ActionResult Index(string myName)
        {
            if (String.IsNullOrEmpty(myName))
            {
                ViewBag.Message = "Hello from Parameter-Less Index Method.";
                return View();
            }
            else
            {
                ViewBag.Message = "Hello from Parameterized Index Method. <br/>Hello " + myName;
                return View();
            }
        }


这篇关于如何在ASP.NET MVC 3中重载索引ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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