ASP.NET MVC路由第一(无名)查询字符串参数

本文关键字:无名 查询 字符串 参数 NET MVC 路由 ASP | 更新日期: 2023-09-27 18:15:35

我正在使用ASP创建一个SAP Content-Server。asp.net MVC 5。Sap使用以下方案调用uri来访问Content-Server功能。

http://servername 脚本 ? 命令

参数

第一个queryString值[上面示例uri中的"command"]映射到一个操作方法,而"scipt"应该映射到控制器。

下面的Urihttp://MyServer/MyApp/ContentServer?info& pVersion = 0045, contRep = K1& docId = 361 a524a3ecb5459e0000800099245ec

应该由控制器"ContentServerController"中的动作方法"info"处理

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

你知道怎么做吗?

ASP.NET MVC路由第一(无名)查询字符串参数

如果第一个querystring是操作,您是否尝试过这样做:

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:需要检查querystring长度和恶意字符串

这是获取非标准参数值的方法:

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加上相应的行动方法,或任何解决方案最适合你。

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