Accessing asp.net mvc routing table in an http module - asp.net

Is it possible to access the routing table in an HttpModule.
But, in fact, more specifically is it possible to programatically figure out which route was taken?

Glimpse will also show you which route was selected, and it's source will also show you how to get access to the currently selected route.

Technically this is possible.
Check out Haack's Route Debugger code. It may help you gain insight into how to accomplish this for your specific needs.
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
Not only can you get this bits to this, it is also a nuget package as well.
I think this will point you in the right direction to get started.

Related

Subdomain routing in ASP.NET 5

I am trying to get sub-domain routing working in my ASP.NET 5 application.
I basically want to map http://api.example.com to a particular controller within my application, http://map.example.com to a different controller etc.
I have looked at various articles about this but they are all out of date and do not work with the latest version of the ASP.NET framework.
The official documentation is missing information on routing. Maybe it's my own fault for trying to use a beta product!
Looking forward to hearing some ideas of how to get this working.
If I understand your intent correctly, ASP.NET routing is not the proper way for this.
If this was for ASP.NET versions before 5.0, I'd have suggested that you intercept the request before it's routed within Application_BeginRequest() and check for HTTP_HOST Request Header value to determine which site the user wanted to visit.
I see that the application flow has changed in a major way with vNext. However, I came across this sample from the ASP.NET MVC 6 source on github that creates a custom route based on a "User" header in the request:
https://github.com/aspnet/Mvc/tree/dev/samples/CustomRouteSample.Web
I believe this may be the starting template for a similar solution to your issue if you use "HTTP_HOST" header instead.
Good luck, let us know if you're able to implement a working solution.

How does Spring work with apparently no code?

I am trying to understand the example described here.
What is puzzling to me is that there is apparently no code. I run the spring-security-samples-insecuremvc-3.2.x within Tomcat and a form is presented to interact with but there is no Java code whatsoever as far as I can see. I can't even find the form. I realize that the code must be in one of the dependencies but I am baffled at how this all works.
Spring does generate/provide a default login form if it's configured to do form-based login and no custom login form is configured.
Sidenote: how to create and configure a custom login form is, for example, explained here: http://docs.spring.io/spring-security/site/docs/3.2.x/guides/form.html (to long to be copy/pasted here).
I have been looking at this sample today with the same questions as you - where the heck is the source? I finally found the answer I think you're looking for. Take a look at the pom.xml and in the dependencies you'll see spring-security-samples-messages-jc. Assuming you downloaded all the samples, import that project into STS and you'll find the files you're looking for.
BTW, I've been following the instructions on the spring security website to add security to this sample and I have not yet been able to get it to work - the login screen does not appear. Let me know if you have better luck than me.
The default spring security login form is generated by the class org.springframework.security.web.ui.DefaultLoginPageGeneratingFilter which is in spring-security-web-$version.jar
The method is generateLoginPageHtml()

How to register a IHttpModule in Orchard which has access to the DefaultContentManager (or the equivalent)

I have been trying to write something for orchard which will check all requests for "_escaped_fragment_" signifying a google ajax crawling request and will perform a 301 redirect to the correct resource which needs to be looked up using the DefaultContentManager.
I had done something like this in a previous project by extending IHttpModule and registering it using
<httpModules>
<add name="GoogleRedirect" type="MyNameSpace.GoogleRedirect"/>
</httpModules>
but have found that using the same approach I can't get access to the orchard content manager.
ie. the following doesn't work
_contentManager = DependencyResolver.Current.GetService<IContentManager>();
I also noticed that Orchard uses Autofac and have been trying to get a line similar to ContainerBuilder().Build().Resolve<IContentManager>() working but the ContainerBuilder doesn't seem to be exposed anywhere?
Is there any way of accessing the DefaultContentManager without having to rewrite the code I currenty have? If not is there a special Orchard way of doing this? What is the easiest way to do it and are there any guides to doing anything similar / any bits of code I can examine? I have been reading through the documentation but not sure of the best way of doing it?
Will carry on reading anyway any help appreciated.
Update
After doing some searching I found the following post:
http://www.deepcode.co.uk/2011/05/real-world-orchard-cms-part-4-cleaning.html
Which has an example of using filters. Just in case anyone else has any problems, I found that my Themes folder was never hitting any of the break points. After comparing with another project I noticed that the theme I had created did not have its own project file. To sort this out I recreated my theme using
codegen theme MyTheme /BasedOn:TheThemeMachine /CreateProject:true /IncludeInSolution:true
No, I don't think this is possible. Any particular reason why you need to do this in a module rather than, say, a filter?

Application_AuthenticateRequest hit for all requests, including images and js files

In my MVC3 application, I'm using Application_AuthenticateRequest to create my custom user context and create the session. However, I notice that this is getting fired for every file per page request, including images, js, css, etc.
Is this the right method to do what I'm trying to do, or should I be doing this somewhere else (i.e. action filter)? Or, is this the right place, I just need to put some checks and/or configuration to ensure this method (or my block of code) is just executed for page requests instead of requests for static files?
I searched for a while trying to find the answer, and found one specific to IIS7, but this is happening for me on my ASP.NET dev server (debugging) on WinXP. Other than that, I couldn't find much, which leads me to think I may be way off on something here, possibly overlooking something simple.
Thanks in advance.
Jerad,
You are correct that you would be better off creating an action filter to handle your user context. You can decorate those controllers where the user context is required.
This is a better solution than using code to investigate the request, just so you can ignore particular requests.
counsellorben

How to construct expandable website structure?

HI,
I have a ASP.NET webiste I created from craft and it now look a big mess. I want to reorganize this but don't know the good way to do it. Some first look well but later cause trouble with master page, image path...
Now I'm thinking of 2 ways:
Using UrlWriter: but it seems lead to a bulk of path rewrite and usually lead to Resource not found or something
Using a page as main entry and using Server.Tranfer to pull the right page content, despite of its location
Which is better? Do you have another method?
Please help!
There's another approach, System.Web.Routing, added in ASP.NET 3.5 SP1. Basically, you implement the IRouteHandler interface and manually route the request to an appropriate handler.
This is how ASP.NET MVC handles request routing. There's a guide here that uses it for Web forms.
By the way, consider looking at ASP.NET MVC and check if it's appropriate for your situation.

Resources