ASP.Net 4.5 WebForms返回重定向到URL(ASP.Net 4.5 WebForms Return Redirect to URL)

编程入门 行业动态 更新时间:2024-10-27 07:16:44
ASP.Net 4.5 WebForms返回重定向到URL(ASP.Net 4.5 WebForms Return Redirect to URL)

有没有办法像在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(); } }

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

return Response.Redirect("~bills/create

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

Is there a way to return a redirect to URL in WebForms the same way you can in MVC. I have a simple function that returns a null if the list is empty and returns the list if there are items in the list. I want to be able to redirect to another page in the same website if the list is null.

Here is the function

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

Instead of returning null, I want to return something like this

return Response.Redirect("~bills/create

But this is not working, it is giving me a red squigly line that will not go away. This is VS2012 ASP.net WebForms Project

最满意答案

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

Response.Redirect("mypage.aspx");

您需要提供最终扩展名为.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(); } }

You can redirect to another aspx page like this

Response.Redirect("mypage.aspx");

you need to provide your page name with .aspx extension in end unlike that in MVC where you needed to provide the URL.

Edited :

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

更多推荐

null,WebForms,return,bills,电脑培训,计算机培训,IT培训"/> <meta name="de

本文发布于:2023-07-22 07:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1217926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   ASP   Net   WebForms   Redirect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!