简体   繁体   English

如何通过字符串调用Controller和View作为ActionResult的结果?

[英]How can I Call Controller and View by string as result for ActionResult?

I'm trying to call controller and a view from it dynamically, I'm retrieving controller name and view name from database, then I want to execute that as the view result of Page/Index url. 我正在尝试动态地调用控制器和它的视图,我正在从数据库中检索控制器名称和视图名称,然后我想将其作为Page / Index url的视图结果执行。

Basically I'm trying to do something like this: 基本上我正在尝试做这样的事情:

public class PageController : Controller
{
    public ActionResult Index()
    {
        var controllerName = // logic to get Controller name (already have)
        var ViewName = // logic to get View name (already have)

        Return View(ControllerName, ViewName); // I want to achieve this functionality without redirect

    }

}

I have tried Server.TransferRequest but that causes HttpContext.Items to be cleared which I don't want it to happen, also I don't want to redirect, Is there any other way? 我已经尝试过Server.TransferRequest但是这会导致HttpContext.Items被清除,我不希望它发生,我也不想重定向,还有其他方法吗?

So presuming, you want to return a the result of an action based on strings. 因此,假设您希望返回基于字符串的操作结果。 You could use reflection to do this. 你可以使用反射来做到这一点。 So something along the lines of: 所以有些东西:

public ActionResult Index()
{
    var controllerName = // logic to get Controller name (already have)
    var viewName = // logic to get View name (already have)


    //get the current assembly
    Type t = typeof(PageController);
    Assembly assemFromType = t.Assembly;

    //get the controller type from the assembly
    Type controllerType = assemFromType.GetType("Namespece." + controllerName);

    //get the action method info
    MethodInfo actionMethodInfo = controllerType.GetMethods(viewName);

    //create an instance of the controller
    object controllerInstance = Activator.CreateInstance(controllerType, null);
    //invoke the action on the controller instance
    return (ActionResult)actionMethodInfo.Invoke(controllerInstance, null);
}

This is untested and TBH I wouldn't recommend doing this . 这是未经测试的TBH 我不建议这样做 It's far from efficent.. there also an assumption here that your action doesn't need parameters which may or may not be true. 它远没有效果......这里也有一个假设,你的行动不需要可能或可能不是真的参数。 The other option (and almost definitely the better option) is to use a Redirect as discussed by other people . 另一种选择(几乎肯定是更好的选择)是使用其他人讨论Redirect

With this MVC still might have problems locating the view. 使用此MVC仍然可能在查找视图时遇到问题。 You may also need to hard code your view path in the action you want to invoke. 您可能还需要在要调用的操作中对视图路径进行硬编码。 Basically what you want is very problematic and have I mentioned I wouldn't recommend doing this 基本上你想要的是非常有问题的,我提到我不建议这样做

It is simple...But i think of some other reason you want something like this.. 这很简单......但我想到你想要这样的东西的其他原因..

public class PageController
{
    public ActionResult Index()
    {
        //Let 
        var controllerName = "Common"// logic to get Controller name (already have)
        var ViewName = "Index" // logic to get View name (already have)

        return RedirectToAction("Index", "Common", new { id = "1" }); 

    }

}

and in Controller 在控制器中

public class CommonController : Controller
    {
        // Initialize with Default value
        public ActionResult Index(String id = "0")
        {
            //Call from PageController
            if (id == "1")
            {
                //Do some stuff
            }
            //Do other stuff of CommonController 
            return View();
        }
 }

I bet you are looking for RedirectToAction("ActionName", "ControllerName"); 我敢打赌你正在寻找RedirectToAction("ActionName", "ControllerName");

I have used the method below to get a string version of a view. 我使用下面的方法来获取视图的字符串版本。 Perhaps you can modify it to return a view based on your controller and view. 也许您可以修改它以根据您的控制器和视图返回视图。 The part where it returns IView. 返回IView的部分。

protected string RenderPartialViewToString(string viewName, object model)
{
    if (string.IsNullOrEmpty(viewName))
        viewName = ControllerContext.RouteData.GetRequiredString("action");

    ViewData.Model = model;

    using (StringWriter sw = new StringWriter())
    {
        ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);

        return sw.GetStringBuilder().ToString();
    }
}

I found a solution: https://stackoverflow.com/a/24475934/5485841 我找到了一个解决方案: https//stackoverflow.com/a/24475934/5485841

you can return a dummy view that has Html.RenderAction inside it. 你可以返回一个内部有Html.RenderAction的虚拟视图。 You can pass controller name and view name by ViewBag or Model. 您可以通过ViewBag或Model传递控制器名称和视图名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM