家庭控制器中的重载方法和ASP.NET MVC中的路由问题 [英] Method over loading in homecontroller and routing issue in asp.net mvc

查看:68
本文介绍了家庭控制器中的重载方法和ASP.NET MVC中的路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有家庭控制器.我的家庭控制器有两种索引方法,一种没有参数,另一种带有参数.就像

i have home controller. my home controller has two index method one with no parameter and one with parameter. it is like

    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        return View("index");
    }

    public ActionResult Index(int a)
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC! and Your Age is " + a;
        return View();
    }

我在global.asax中仅定义了两个路由条目,例如

i have define only two route entry in global.asax like

            routes.MapRoute(
                "Default1", // Route name
                "{Home}/{ID}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

我希望当我输入 http://localhost:7221 http://localhost:7221/home 之类的url时,再使用没有参数的index()方法家用控制器的控制器应触发并

I want that when i type url like http://localhost:7221 or http://localhost:7221/home then index() method with no param of home controller should fire and

当我键入 http://localhost:7221/home/77 时,应该触发带有home控制器参数的index(int a)方法.但是我收到错误提示,为什么我输入两种指定的网址.错误消息是在以下操作方法之间,当前对控制器类型"HomeController"的索引"操作请求是不明确的:类型BeginnerMVC.Controllers.HomeController上的System.Web.Mvc.ActionResult Index()类型BeginnerMVC.Controllers.HomeController上的System.Web.Mvc.ActionResult Index(Int32)

when i type http://localhost:7221/home/77 then index(int a) method with param of home controller should fire. but i am getting error why i type two kind of url i specified. the error message is The current request for action 'Index' on controller type 'HomeController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Index() on type BeginnerMVC.Controllers.HomeController System.Web.Mvc.ActionResult Index(Int32) on type BeginnerMVC.Controllers.HomeController

我无法捕获该错误.我的路由代码有什么问题.请帮忙.谢谢

i am not being able to catch the error. what is wrong in my routing code. please help. thanks

推荐答案

您不能在MVC中将方法重载与操作一起使用.如果您详细说明了要实现的目标,则将获得有关如何做到这一点的建议.

You can not use method overloading with actions in MVC. If you elaborate what you're trying to achieve, you would get advices on how to do that.

这篇关于家庭控制器中的重载方法和ASP.NET MVC中的路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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