【问题标题】:Remove characters from request.querystring从 request.querystring 中删除字符
【发布时间】:2014-08-01 17:37:24
【问题描述】:

我有一些代码可以从下面的 url 中获取 DrawingId:

/Queue/Queue.aspx?Mode=Autosearch&DrawingID=188320"

我的问题是在运行程序的另一部分后,该网址变成了这样:

/Queue/Queue.aspx?Mode=Autosearch&DrawingID=188320?DrawingRequestId=376333&doQuerySearch=true&start=&end=&Ind=&Model=&status=Open&manager=&source=&assignee=&title=&features=&parts=&pas=&customer=&number=&project= &startIndex=1&endIndex=10&pageSize=10&total=193&date=Extended&qsEnd=

188320之后有什么好办法去掉吗?还有一种提取 376333 的 DrawingRequestId 的好方法吗?

这是 DrawingID 的代码:

public string DrawingId
{
  get
  {
   if (Request.QueryString["DrawingID"] != "" && Request.QueryString["DrawingID"] != null)
   {
     return Request.QueryString["DrawingID"];
   }
  return null;
  }
}

这部分不会运行,因为 DrawingId 不正确:

List<string> featureCodes = drawingBusiness.GetFeatureCodesByDrawingId(long.Parse(DrawingId));

此代码不起作用,因为 DrawingId 不是 188320,

变成 188320?DrawingRequestId=376333

这是我认为问题开始的 javascript:

function CloseAndRefresh() {
        var parentUrl = window.top.location.href;
        if (parentUrl.indexOf("Queue/Queue.aspx") != -1) {

            if (window.location.search === "") {
                window.location.href = window.top.location
            }
            else {
                window.top.location.href = window.top.location + window.location.search;
            }
        }
        else {
            if (window.location.search === "") {
                window.location.href = window.top.location
            }
            else {
                window.top.location.href = window.top.location + window.location.search;
            }
        }
    }

【问题讨论】:

标签: c# javascript asp.net request.querystring


【解决方案1】:

要删除附加到您的网址的所有代码,您可以执行以下操作:

var longURL = "/Queue/Queue.aspx?Mode=Autosearch&DrawingID=188320?DrawingRequestId=376333&doQuerySearch=true&start=&end=&Ind=&Model=&status=Open&manager=&source=&assignee=&title=&features=&parts=&pas=&customer=&number=&project=&startIndex=1&endIndex=10&pageSize=10&total=193&date=Extended&qsEnd=";

var shortURL = longURL.split('?').slice(0, -1).join('?');

console.log(shortURL) // "/Queue/Queue.aspx?Mode=Autosearch&DrawingID=188320"

要提取DrawingRequestId,您可以执行以下操作:

var longURL = "/Queue/Queue.aspx?Mode=Autosearch&DrawingID=188320?DrawingRequestId=376333&doQuerySearch=true&start=&end=&Ind=&Model=&status=Open&manager=&source=&assignee=&title=&features=&parts=&pas=&customer=&number=&project=&startIndex=1&endIndex=10&pageSize=10&total=193&date=Extended&qsEnd=";

var drawingRequestId = parseInt(longURL.substr(longURL.indexOf('DrawingRequestId=')+17, 6));

console.log(drawingRequestId) // 376333

【讨论】:

  • 你好,感谢这个例子。所以那个url:/Queue/Queue.aspx?Mode=Autosearch&DrawingID=188320 是我的window.top.location。我仍然不完全理解这是如何工作的。如果我只想抓住 /Queue/Queue.aspx 我该怎么做呢?您知道我可以将 window.top.location 设置为仅指向 queue.aspx 吗?
  • 我有一个需要该 DrawingID 的函数,我认为如果有办法在该函数运行后清除“Queue.aspx”之后的所有内容,我的问题将真正得到解决。
  • 在我的回答中使用相同的longURL,您可以这样做:window.top.location.href = longURL.split('?')[0];,但请记住,如果您这样做,页面将重新加载。
  • 我最终将 window.top.location 设置为指向我正在使用的文件,因此刷新了查询字符串,这解决了我的问题。你的回答确实回答了这个问题。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2016-07-26
  • 2013-09-18
  • 2011-11-22
  • 1970-01-01
相关资源
最近更新 更多