【问题标题】:overriding Method asp.net mvc覆盖方法 asp.net mvc
【发布时间】:2011-02-11 19:27:27
【问题描述】:

我可以重写 ActionResult 方法吗?假设我在 AccountController 中有一个方法 Index 这样的

公共行动结果索引() {

返回视图(); }

我可以再多一种方法名称相同但参数不同吗 喜欢 公共 ActionResult 索引(int userid) {

返回视图(); }

我不想这样称呼它http://something/accounts/index/11 我只想说http://something/accounts/11

你也可以看看 stackoverflow 的东西 如果你去https://stackoverflow.com/users 我觉得用户是控制器,默认操作是索引,所以不要显式地调用它。 现在如果你输入类似https://stackoverflow.com/users/96346/parminder 两个参数分别是96346和parminder

我希望这是有道理的。

global.asax 中的条目是什么

问候 帕尔米德

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

    你可以做动作重载(两个动作在同一个控制器上使用不同的参数),但它们应该可以在不同的 HTTP 动词上调用。这是 ASP.NET MVC 应用程序中的一种常见模式:一个可通过 GET 访问的操作,该操作呈现一个包含表单的视图,另一个操作是该表单将发布到的:

    public class HomeController: Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        [HttpPost] // [AcceptVerbs(HttpVerbs.Post)] for ASP.NET MVC 1.0
        public ActionResult Index(SomeViewModel model)
        {
            if (ModelState.IsValid)
            {
                // TODO: validation passed => do something with the model
            }
            return RedirectToAction("index");
        }
    }
    

    无需为此修改您的路线,它适用于默认路线。

    【讨论】:

    • 谢谢达林,我知道这些东西。但要求不同。问候
    【解决方案2】:

    看看我的问题here

    我认为你可以通过修改一些方法属性来做到这一点,但我认为如果你编写另一个方法然后为其添加路由会更简洁。它看起来像这样:

    public ActionResult Index()
    {
        return View();
    } 
    
    public ActionResult UserInfo(int id)
    {
        //some stuff
        return View(modelObject);
    }
    

    然后你为它提供一条路线,例如:

    routes.MapRoute("UserInfo", //route name
                    "Accounts/{id}",
                    new 
                    { 
                        controller = "Accounts",
                        action = "UserInfo",
                        id=UrlParameter.Optional 
                    });
    

    【讨论】:

    • 嗨纳西尔,感谢您的回答。我试过这种方法。但我不能这样称呼我的方法something/accounts/1 它不能那样工作。是的,如果我写accounts/userinfo/1 谢谢
    • 如果你正确映射了路由,你可以直接从 accounts/3 调用你的方法。你能发布你的 Global.asax 吗?
    • 您好 Narsil,我删除了默认路由。这是我的 global.asax routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute("UserInfo", //路由名称 "Users/{id}/{name}", new { controller = "Users", action = "UserInfo", id = UrlParameter.Optional, name = UrlParameter.Optional } );这是控制器方法 public ActionResult UserInfo(int? id, string name) { return View(); } 现在可以使用了,谢谢
    【解决方案3】:

    您想过度加载该方法。但你不需要。

    只要让参数可以为空:

    public ActionResult Index(int? userid)
    {
       return (userid.HasValue) ? ShowUser(userid.Value) : ShowOverview();
    }
    

    本例可以采用默认路由图。

    对于您(更改的)要求,您确实需要使用约束来修改您的路线。

    routes.MapRoute("directaccount", "Accounts/{userid}/{someotherparam}",
        new { controller="Accounts", action="ShowAccountByID", someotherparam=null }
        new { userid=@"\d+"});
    
    *here goes the rest*
    

    reg-ex 确保您的其他呼叫不会被此路由吃掉。这是未经测试的。我不建议为此功能使用重载。

    【讨论】:

    • 您好 Wikser,非常感谢您的回答。默认索引有效,但是当我必须传递参数时,我不得不说something/accounts/index/1 我不想这样做。问候
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多