How to use MVC results Results from regular web forms (fake ControllerContext)?

We have a plugin framework that returns objects ActionResultthat display things in the browser. One of the last requirements is that our plugins must be called from a regular ASP.NET Web Forms application.

So far I have come up with this that works for very simple ActionResults:

public class ActionResultTranslator {

    HttpContextBase _context;

    public ActionResultTranslator(HttpContextBase context ) {

        _context = context;
    }

    public void Execute(ActionResult actionResult) {

        ControllerContext fakeContext = new ControllerContext();
        fakeContext.HttpContext = _context;            

        actionResult.ExecuteResult(fakeContext);        
    }
}

You could call this from a web form with:

protected void Page_Load(object sender, EventArgs e) {
   HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);
   var translator = new ActionResultTranslator(contextWrapper);
   translator.Execute(new RedirectResult("http://google.com"));     
}

What else do I need to do to intercept everything? For example, what if I want to return a ViewResult?

+3
source share
1 answer

There are not too many fake properties in ControllerContext.

  • HttpContext.
  • . , ActionResults , null
  • RequestContext - , null
  • RouteData​​strong > . , ​​.

, , ActionResult , RouteData. ViewResult , , , . , RouteData , .

+1

Source: https://habr.com/ru/post/1720886/


All Articles