有没有办法像在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/createBut 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
发布评论