ASP.NET MVC Route by first(无名)查询字符串参数

时间:2016-10-31 12:32:08

标签: c# asp.net asp.net-mvc

我正在使用ASP.NET MVC 5创建SAP Content-Server。 Sap使用以下方案调用URI以访问Content-Server函数。

http://servername:port/ 脚本 命令&安培;参数

第一个queryString值[示例-Uri中的“command”]映射到action-method,而“scipt”应该映射控制器。

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

应由控制器“ContentServerController”

中的action-method“info”处理
 public class ContentServerController : Controller
 {            
     public ActionResult info(string contRep, string docId, string pVersion)
     {
         throw new NotImplementedException();
     }

任何想法如何做到这一点?

3 个答案:

答案 0 :(得分:0)

如果是第一个查询字符串,那么您是否尝试过这样做:

public class ContentServerController : Controller
{
    public ActionResult Index(string contRep, string docId, string pVersion)
    {
        return RedirectToAction(Request.QueryString[0]);
    }

    public ActionResult info(string contRep, string docId, string pVersion)
    {
        return Content("info action");
    }
}

PS:你需要检查查询字符串长度和恶意字符串。

答案 1 :(得分:0)

这是你如何掌握非标准参数值:

public class ContentServerController : Controller
{
    public ActionResult Index()
    {
        var data = new List<string>();
        foreach (string key in Request.QueryString.Keys)
        {
            data.Add("key=[" + (key ?? "--null--") + "], value=[" + Request.QueryString[key] + "]");
        }
        return View(data);
    }
}

...结合此Index视图:

@model List<string>
<a href="/ContentServer?info&pVersion=0045&contRep=K1&docId=361A524A3ECB5459E0000800099245EC">Link 1</a><br />
<a href="/ContentServer?about&foo=true&bar=false">Link 2</a><br />
<a href="/ContentServer?login&secure=yes">Link 3</a><br />
<hr size="1" />
@if (Model != null)
{
    foreach (var str in Model)
    {
        @str<br />
    }
}

由此决定是否要在switch()Request.QueryString[null]以及相应的操作方法或最适合您的解决方案上执行大RedirectToAction("~/" + Request.QueryString[null]?" + all other parameters声明。

答案 2 :(得分:0)

fabiosilvalima的答案帮助我解决了这个问题

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();
     }  

}