How to reuse this code in C #?

I cannot find a way to reuse code on all my web pages.

How can i do this?

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Page.Request.UrlReferrer == null) { Response.Redirect("test.aspx"); } } } 

I would like to use something like this:

 protected void Page_Load(object sender, EventArgs e) { CheckURL(); } 

Please give me an example! :) I am using C #!

0
source share
2 answers

Just create a base page where all other pages are inherited. Put your code on the base page.

so public class CurrentPage : BasePage (inherits)

then

public abstract class BasePage : System.Web.Ui.Page

+4
source

You can create a new class that inherits from System.Web.UI.Page , and add this piece of logic here, and then use the new class for all of your pages.

EDIT:

something like that

  public class MyPage : System.Web.UI.Page { public MyPage() { Load += MyPage_Load; } void MyPage_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Page.Request.UrlReferrer == null) { Response.Redirect("test.aspx"); } } } } 

and add this to web.config

 <system.web> <!-- ... --> <pages pageBaseType="MyNamespace.MyPage" /> <!-- ... --> </system.web> 
+2
source

Source: https://habr.com/ru/post/1436697/


All Articles