ASP.Net 4.5 WebForms 返回重定向到 URL

Val*_*for 1 asp.net webforms

有没有办法像在 MVC 中一样在 WebForms 中返回到 URL 的重定向。我有一个简单的函数,如果列表为空,则返回 null,如果列表中有项目,则返回列表。如果列表为空,我希望能够重定向到同一网站中的另一个页面。

这是函数

BillContext _context = new BillContext();

public List<Models.Bill> GetBills()
{
    var bills = from b in _context.Bills
                where b.UserName == HttpContext.Current.User.Identity.Name
                select b;

    if (bills.ToList().Count() < 1)
    {
        return null;
    }
    else
    {
        return bills.ToList();
    }
}
Run Code Online (Sandbox Code Playgroud)

我想返回这样的东西,而不是返回 null

return Response.Redirect("~bills/create
Run Code Online (Sandbox Code Playgroud)

但这不起作用,它给了我一条不会消失的红色波浪线。这是 VS2012 ASP.net WebForms 项目

Sid*_*d M 5

您可以像这样重定向到另一个 aspx 页面

Response.Redirect("mypage.aspx");
Run Code Online (Sandbox Code Playgroud)

您需要提供带有.aspx扩展名的页面名称,这与 MVC 中需要提供 URL 的名称不同。

编辑:

public List<Models.Bill> GetBills()
{
    var bills = from b in _context.Bills
                where b.UserName == HttpContext.Current.User.Identity.Name
                select b;

    if (bills.ToList().Count() < 1)
    {
        HttpContext.Current.Response.Redirect("mypage.aspx");
        return null;
    }
    else
    {
        return bills.ToList();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 尝试 `HttpContext.Current.Response.Redirect("mypage.aspx);` 而不是 `Response.Redirect("mypage.aspx");` (2认同)