I have a webforms project, and I'm trying to run some code that allows me to make a call to an MVC route, and then display the result in the body of the web forms page.
There are several HttpResponse / Request / Context shells that I use to make a call to the MVC route, for example:
private static string RenderInternal(string path) { var responseWriter = new StringWriter(); var mvcResponse = new MvcPlayerHttpResponseWrapper(responseWriter, PageRenderer.CurrentPageId); var mvcRequest = new MvcPlayerHttpRequestWrapper(Request, path); var mvcContext = new MvcPlayerHttpContextWrapper(Context, mvcResponse, mvcRequest); lock (HttpContext.Current) { new MvcHttpHandlerWrapper().PublicProcessRequest(mvcContext); } ...
The code works great for performing simple MVC routes, for example. "/ Home / Index". But I canโt specify the query string parameters (for example, "/ Home / Index? Foo = bar"), because they are simply ignored. I tried setting QueryString directly in the RequestWrapper instance, for example:
public class MvcPlayerHttpRequestWrapper : HttpRequestWrapper { private readonly string _path; private readonly NameValueCollection query = new NameValueCollection(); public MvcPlayerHttpRequestWrapper(HttpRequest httpRequest, string path) : base(httpRequest) { var parts = path.Split('?'); if (parts.Length > 1) { query = ExtractQueryString(parts[1]); } _path = parts[0]; } public override string Path { get { return _path; } } public override NameValueCollection QueryString { get { return query; } } ...
When debugging, I see that the correct values โโare in "request.QueryString", but the values โโare never bound to the method parameter.
Does anyone know how QueryString values โโare used and bound from an HTTP request to an MVC controller action?
It seems that handling a QueryString value is more complicated than I expected. I have limited knowledge of the internal components of the MVC query pipeline.
I try to explore the insides on my own and will continue to do so. If I find anything, I will update this post accordingly.
I also created a very simple web form project containing only the code needed to create this problem, and shared it using Dropbox: https://www.dropbox.com/s/vi6erzw24813zq1/StackMvcGetQuestion.zip The project just contains one Default page .aspx, the controller and the MvcWrapper class, used to render the result of the MVC path. If you look at Default.aspx.cs, you will see the route path containing the querystring parameter, which is passed, but it is never associated with the action parameter.
As a quick reference, here are some excerpts from this web project.
Controller:
public class HomeController : Controller { public ActionResult Index(string foo) { return Content(string.Format("<p>foo = {0}</p>", foo)); } }
Default.aspx page:
protected void Page_Load(object sender, EventArgs e) { string path = "/Home/Index?foo=baz"; divMvcOutput.InnerHtml = MvcWrapper.MvcPlayerFunctions.Render(path); }
I struggled with this for a long time, so I would be grateful for any advice in any form. :)