扩展ASP.NET MVC的操作方法,怎么办返回查看?

编程入门 行业动态 更新时间:2024-10-24 07:35:34
本文介绍了扩展ASP.NET MVC的操作方法,怎么办返回查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

老:

public class HomeController : Controller { public ActionResult Index() { // do something return View(); } }

我想延长索引()

I want extend "Index()" :

public static class HomeControllerExtensions{ public static ActionResult Index(this HomeController hc,string viewName) { // do something return View(viewName);//hc.View cannot...., how to do return View()? }}

怎么办返回查看()?

how to do return View()?

推荐答案

您想要做的是不常见的。如果你写更多关于你想要什么,我们可以帮助更好。下面是你想要做什么。视图是由System.Web.Mvc.Html.Action调用。也就是说,如果在$ C $下面c是为一些有益的,只能在控制器中使用。在这个例子中我在SomeController调用动作关于控制器家用你要创建的扩展。

What you want to do is not common. If you write more about what you want exactly, we can help better. Below is what you're trying to do. A view is invoked by System.Web.Mvc.Html.Action. That is, if the code below is useful for something, can only be used in a controller. In the example I'm in the SomeController calling the action "About" the controller "Home" using the extension you want to create.

该扩展code:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; using System.Web.Routing; namespace StackOverflow.Controllers { public static class ControllersExtensions { public static ActionResult Index(this HomeController controller, string ViewName) { string controllerName = controller.GetType().Name.Replace("Controller", ""); RouteValueDictionary route = new RouteValueDictionary(new { action = ViewName, controller = controllerName }); RedirectToRouteResult ret = new RedirectToRouteResult(route); return ret; } } }

该SomeController:

The SomeController:

namespace StackOverflow.Controllers { public class SomeController : Controller { // // GET: /Some/ public ActionResult Index() { HomeController home = new HomeController(); return home.Index("About"); } } }

Home控制器:

The Home Controller:

public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; return View(); } public ActionResult About() { ViewBag.Message = "Your app description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } }

更多推荐

扩展ASP.NET MVC的操作方法,怎么办返回查看?

本文发布于:2023-11-05 07:47:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作方法   ASP   NET   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!