【问题标题】:Call handler without Response.Redirect? [duplicate]没有 Response.Redirect 的调用处理程序? [复制]
【发布时间】:2011-08-18 21:13:46
【问题描述】:

可能重复:
How do I call an ASHX from inside an ASPX.VB function?

看看我下面的代码,我的问题很明显,如何在不重定向的情况下调用ashx文件?我需要按此顺序执行代码,但是当页面重定向时,重定向下方的所有内容都不会执行。提前致谢。

protected void btnAddMach_Click(object sender, EventArgs e)
{
    InsertMachine();

    if (cbMachIcal.Checked && !cbMachGcal.Checked)
    {
        Response.Redirect("iCal.ashx?type=Lead&leadID=" + Convert.ToInt32(hfLeadID.Value) + "&date=" +
                           txtMachTickDate.Text + "&time=" + txtMachTickTime.Text + "&user=" +
                           Request.Cookies["upProspektor"]["userName"] + "&model=" +
                           ddlModel.SelectedItem.Text);
    }
    else if (!cbMachIcal.Checked && cbMachGcal.Checked)
        CreateGcalEvent();
    else if (cbMachIcal.Checked && cbMachGcal.Checked)
    {
        CreateGcalEvent();
        Response.Redirect("iCal.ashx?type=Lead&leadID=" + Convert.ToInt32(hfLeadID.Value) + "&date=" +
                           txtLeadTickDate.Text + "&time=" + txtLeadTickTime.Text + "&user=" +
                           Request.Cookies["upProspektor"]["userName"] + "&model=" +
                           ddlModel.SelectedItem.Text);
    }

    ClearControls(upMachDetails);
    UpdateHasMach();

    btnUpComm.Style.Add("display", "none");
    btnNewMach.Style.Add("display", "none");
    btnUpMach.Style.Add("display", "none");

    if (hfAddMach.Value.ToString() == string.Empty)
    {
        hfAddMach.Value = "1";
        dvPl = dvProLeads(cmdProLeads());
        gvProLeads.DataSource = dvPl;
        gvProLeads.DataBind();
        this.upReports.Update();
    }

    Utilities.DisplayAlert(this, this.GetType(), "Machine Info Entered Successfully!");
}

【问题讨论】:

  • 感谢您的快速回复,并对重复的帖子感到抱歉。我不是全职开发人员,所以重构是新事物,我听说过这个术语并看到工具栏上的菜单项,但仅此而已。关于如何开始的任何指示?
  • 重构代码基本上意味着重新思考/重写它。如果你有很多重复的代码,或者需要在逻辑上重构一些东西,你重写代码来尝试解决这些问题。
  • 基本上,您希望获取 ashx 中的代码并将核心功能移动到一个通用组件中,很可能是库项目中的一个类。然后直接从后面的代码调用该类,而不是尝试通过 http 调用 ASHX。

标签: c# asp.net httphandler


【解决方案1】:

我最初的想法是,这听起来像是一个有缺陷的设计。您将浏览器重定向到 ASHX 文件,然后尝试继续执行当前页面(包括将内容输出到页面)。我建议重构代码来解决这个问题。

...但是...

如果您绝对必须在页面的早期重定向并且仍然在原始页面中执行某些代码,您可以使用Response.Redirect(string, bool) 使页面在调用时不会自动停止执行。

protected void btnButton_Click(object sender, EventArgs e){
   if(redirect_flag){
      Response.Redirect(newUrl, false);
   }
   // This will still execute, as well as the remainder of the entire page
}

我仍然建议重构代码而不是这种方法。

【讨论】:

    猜你喜欢
    • 2019-07-12
    • 1970-01-01
    • 2010-12-27
    • 2019-11-19
    • 1970-01-01
    • 2011-01-31
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多