Markus1980Wien Markus1980Wien - 1 month ago 12
ASP.NET (C#) Question

ASP.NET MVC Route by first (nameless) query string-parameter

I am creating a SAP Content-Server using ASP.NET MVC 5.
Sap calls URIs with the following scheme to access Content-Server functions.

http://servername:port/script?command&parameters

The first queryString value ["command" in the example-Uri above] maps to a action-method, while "scipt" should map the controller.

The following Uri
http://MyServer/MyApp/ContentServer?info&pVersion=0045&contRep=K1&docId=361A524A3ECB5459E0000800099245EC

should be handled by the action-method "info" in controller "ContentServerController"

public class ContentServerController : Controller
{
public ActionResult info(string contRep, string docId, string pVersion)
{
throw new NotImplementedException();
}


Any ideas how this can be done?

Answer

The answer of fabiosilvalima helped me to solve the problem

public class ContentServerController : Controller
{  
     // one public Action-Method which reads the "command" from query-string
     // and calls different private methods according to commandName          
     public ActionResult ContentServer(string contRep, string docId, string pVersion)
     {
         string commandName = Request.QueryString[0];
         switch(commandName)
         {
             case "info":
                 return info(contRep, docId, pVersion);
             case "get":
                 return get(contRep, docId, pVersion);
             case "create":
                 return create(contRep, docId);
             default:
                 throw new NotImplementedException();
         }

     private ActionResult info(string contRep, string docId, string pVersion)
     {
         throw new NotImplementedException();
     }
     private ActionResult get(string contRep, string docId, string pVersion)
     {
         throw new NotImplementedException();
     }  
     private ActionResult create(string contRep, string docId)
     {
         throw new NotImplementedException();
     }  

}