Search code examples
asp.netasp.net-core-mvcasp.net-mvc-routing

Using reserved word "action" as an MVC parameter name


To provide details regarding my platform. I am using Visual Studio 2017 with a .NET Core Web project.

I am implementing a new back-end for a client for which I cannot alter the client. I am attempting to use MVC to accomplish this.

The URL which I must be able to respond to is -> https://my.server.com:8443/portal/gateway?sessionId=0A6405100000001D04FB55FE&portal=4ba457d0-e371-11e6-92ce-005056873bd0&action=cwa&token=3fcbc246bb3c35a5fa8055fec6bbf431

I would like to extract the values for :

  • sessionId
  • portal
  • action
  • token

As they all have meaning.

I have created a new view folder within my project called "Portal" and have created a new MVC controller named "PortalController".

I have created a new View called gateway.cshtml and also have implemented the following function ->

public IActionResult Gateway(string sessionId, string portal, string action, string token)
{
    ViewData["Message"] = "Gateway";
    return View();
}

When I set a breakpoint within the function, I receive the following values for the URL provided above ->

    sessionId   "0A6405100000001D04FB55FE"              string
    portal      "4ba457d0-e371-11e6-92ce-005056873bd0"  string
    action      "gateway"                               string
    token       "3fcbc246bb3c35a5fa8055fec6bbf431"      string

As can be seen in the output of the debugger, for the parameter named "action", isntead of receiving the value "cwa" as passed by the client, I instead receive the name "gateway" which is passed by MVC as the name of the action.

Is there a "proper way" of handling this?

I would prefer to avoid altering the routes within the startup.cs file as this hurts the modularity of the code. I would welcome attributes that could be used to reroute the parameter names.

Another alternative I could see (buy can't figure out) is to simply make use of the HTTP request to read the values I'm interested in instead of using function parameters.

Thank you very much in advance for any assistance you can provide!


Solution

  • In .Net Core you can get your action parameter value straight off the Request with

    var action= Request.Query["action"];
    

    where in .Net Framework it was

    var action= Request.QueryString["action"];