ASP.Net Core MVC Middleware - Get Target URL From Secondary File Requests

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



ASP.Net Core MVC Middleware - Get Target URL From Secondary File Requests



I am trying to use custom middleware to intercept and modify requests to a certain controller (FooController) while letting other requests go through as usual. I am trying to identify these using context.Request.Path as shown:


context.Request.Path


public async Task Invoke(HttpContext context)

if (context.Request.Path.Value.StartsWith("/Foo", StringComparison.OrdinalIgnoreCase))

// do stuff


...



The problem is that navigating to https://localhost/Foo/Index creates several actual requests:


/Foo/Index
/js/foo-script.js
/images/my-image.png



I would like to be able to intercept and modify all of these related requests, and my current approach only catches the first request. The closest question I have been able to find is this Current URL in ASPCore Middleware? but the provided extension methods still don't show the URL that the user typed or the link they clicked... only the file that is currently being retrieved. Is there any property in the HttpContext that will show me the "parent request" for the scripts, images, stylesheets, and other assets referenced by the Index view?



EDIT: I can set a breakpoint and see the middleware being invoked, and I can see that /Foo/Index matches the if statement and that /js/foo-script.js does not, so that part seems to be fine. The middleware is registered in startup.cs like this:


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

app.UseMyMiddleware();

...



using the following extension method as a helper (this part is all working as expected):


public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder builder)

return builder.Use(next => new FooMiddleware(next).Invoke);





Could you please show where in the pipeline is your middleware registered? Is it possible that another middleware in the processing pipeline is handling the request and not calling next RequestDelegate? Do you have static files middleware app.UseStaticFiles();
– ironstone13
Aug 10 at 20:18


RequestDelegate


app.UseStaticFiles();





@ironstone13 - I have updated the question, but the middleware is defintely being hit and working as expected in the case of /Foo/Index, it just misses things like scripts and images that are requested as a result of being included in the Index view's HTML.
– Elemental Pete
Aug 10 at 20:26


/Foo/Index




1 Answer
1



Is there any property in the HttpContext that will show me the "parent request" for the scripts, images, stylesheets, and other assets referenced by the Index view?



Try the "Referer" request header:


public async Task Invoke(HttpContext context)

var path = context.Request.Path;
var referer = context.Request.Headers["Referer"];

Console.WriteLine($"Referer: referer Path: path");

await _next(context);



For instance, if I navigate from the /Bar page to the /Foo page, then I see the following output:


/Bar


/Foo


Referer: https://localhost:5001/Bar Path: /Foo
Referer: https://localhost:5001/Foo Path: /css/site.css



That second line means that /Foo was the "parent request" for the /css/site.css file.


/Foo


/css/site.css





This approach works for the question as stated, so I accepted it as the answer. After some debugging and testing, I found it to be more effective to use a URL Rewrite in IIS instead of trying to handle it with middleware, but this is all still good info! Thanks for your help.
– Elemental Pete
Aug 14 at 15:39






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard