Is there a way to detect routable URLs in ASP.NET?

Is there no way to determine if the current request is displayed using ASP.NET 4.0 URL routing?

I have an HTTP module that handles the BeginRequest event of BeginRequest application. I found that this handler is called for all types of files, including CSS, JS, image files, etc., and I just want to perform the action if the target file is an ASPX page.

In the case of routable pages, all the properties of the HttpRequest object reflect the requested URL, not the ASPX page to which the request is mapped. How to determine if a request will be processed by an aspx file?

Thanks for any suggestions.

+4
source share
2 answers

Inside the Begin Request event, a handler is not defined for a specific URL.

Thus, there is no way to determine what actually ends up processing this URL, since IIS has yet to be resolved. This happens after the Begin Request is run, so all file types are called.

One of the reasons Begin Request is not a good event to actually execute code requires target specific .NET files. A good use of the Begin Request method is to add cookies or headers to the request or response. You can resort to them without problems, regardless of what ends with the processing of the request.

As mentioned earlier, I suggest a base class that inherits from System.Web.UI.Page that inherits all of your other pages, or create a master page.

Now, not knowing that you are trying to do this, it is difficult to give a good solution. It may be possible to check the URL to see if it will be launched by the Route, but I don't know how this also seems excessive when you can handle it through the base class or the main page.

+1
source

You can create BasePage as a base class for all pages of the application and handle the Page_Load event there instead of using the HTTP module.

+1
source

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


All Articles