Wildcard mapping for ASP.NET and issues with PHP - c#

I have an application written in .NET 3.5 with C# as the language. I'm using Web Forms, but using url routing with the routes defined in my global file. Everything is working as expected. In order for the pretty paths (see: user/665 instead of user.aspx?uid=665) to work properly, I had to add a wildcard mapping in IIS5.1 (local box, not test, staging, or production) the aspnet_isapi file for the 2.0 framework. Everything works fine.
Now, my site needs a plugin for PHP. However, the PHP files are now being serviced by ASP.NET due to the wild card mapping, and hence are not processed by the PHP interpretter. Is there any way to get around this? Would I have to add some sort of handler to my web app that will take all PHP requests being handled by the ASP.NET framework and have them routed to the PHP engine? Is there an easier way? Maybe a way to exclude them in the web.config (PHP files) and have them served by the proper PHP engine?
Thanks all!
-Steve

This is a solution, but is not an elegant way (IMHO):
Create a virtual directory
Have it point to the folder with the files (in this case, a PHP plugin)
Give it the proper permissions
Change the config options for the virtual directory in IIS and make sure the wildcard mapping for that directory is removed.
This works like a charm for my situation. However, is there any way to not have to deal with virtual directories?

The problem is that the PHP extension needs to be registered.
In IIS Manager right-click on Default Website -> Properties -> Home Directory -> Configuration
Under Application Mappings make sure that .php is added and is it pointing to PHP.EXE. There should be an entry like this: extension .php, executable path C:\PHP\PHP.EXE %s %s

From what I gather, the problem is that ASP.NET is attempting to route your PHP requests, so what I would do is add a StopRoutingHandler() to your routes in the global.asax. Something like this should work:
routes.Add(new Route("{resource}.php/{*pathInfo}", new StopRoutingHandler()));
Edit: Be mindful that routes are processed in order, so I would add this to the top of your routes.

Related

How to override blazor client configuration with environment variables?

I have working web client, it uses "wwwroot/appsettings.json" file for its configuration.
Now I would like to override just single settings using environment variable for it (as an example, in reality there will be many, arbitrary, overrides). Is there ready to use mechanism, similar to ASP.NET Core server (all it takes is calling extension method and combining json, env. variables)?
I am not asking about multiple .json files and switching between them depending on ENVIRONMENT variable, it is completely different scenario.
So far I didn't find anything even close, so thinking about DIY approach I see an ugly path -- moving client configuration file into hosting server, adding main node in client .json file like "client", using environment variables with prefix "client", merging those data using ASP.NET server mechanism, dumping it back to file for the client usage. And hoping it will work :-).
So I followed DIY path :-) If anyone like it here are the steps:
create 3 files with empty JSON at web client wwwroot -- appsettings.json, appsettings.Development.json and Production version as well
put your entire web client config in appsettings.json at hosting server at "CLIENT" node for example
in your server Startup constructor create configuration as usual, but then fetch entire "CLIENT" section and "jsonize" it back (see: https://stackoverflow.com/a/62533775/6734314) -- convert it to string and write to $"wwwroot/appsettings.{env.EnvironmentName}.json" (where env is IWebHostEnvironment)
And that's it -- I tested it in Development mode and in Production. The only drawbacks I see are:
it looks weird :-)
when executed using VS the written file is put not relative to binary file, but relative to project -- so when you are done you have to delete newly created file (otherwise on the next run VS will complain about conflict between two static files)
You override the settings using env. variables at server side using "CLIENT" prefix, and the rest is as usual.
For the record, I am not saying this is perfect, but I didn't look for anything more than I asked in my question. What I would like to do however is to reduce the number of steps, or even better to use some already existing mechanism. I will be grateful for the improvements/tips within those areas.

Separate MVC application as a folder using Handler Mapping

We have a legacy webforms app and a new section of our site developed with MVC. It works a treat as an application in the Default Web Site in IIS.
Only downside is that the root site's session object isn't passed to the application. We have a work around for this.
A consultant is convinced that we don't need to set it up as an application and it can all be done via handlers and route mappings. He conveniently has not provided us with any details though and I have been tasked to implement it.
The best I have achieved was to add a Module Mapping to the Handler Mapping section in IIS. This filters coolmvcapp/* with Module isapimodule to a virtual directory who's physical path points to our new MVC app. The result is an empty page with a 200 response. Most other combinations I have tried resulted in a variety of errors. (Turns out an empty page is returned without adding the handler mapping - hey ho)
So, is this approach even possible?
I am aware that MVC and webforms can be merged together but for reasons this is something that we want to avoid.

Is there a way to have IIS default document (default.asp) take precedence over .Net routing?

I am trying to migrate a site from classic ASP to .Net (using WebMatrix). I am planning on mixing the new .cshmtl pages in with the legacy .asp files and slowly migrate the site over time. To date, I have individual .asp pages for each piece of content (each url). As I move to .Net, the content will be coming from a database, so there will not be specific pages for each url. I love the power of routing that is available in .Net and it works well and plays nicely with the classic ASP with one exception so far.
My example is that I have an ASP page located at /crafts/default.asp and currently reference that everywhere as just /crafts/. The IIS default document setting takes care of serving the default.asp page when i just reference /crafts/. I want to start to post new items into this area of the site but run as .Net pages. so I created a /crafts.cshtml page which is designed to handle different urls and go to the database and lookup the item and display the info. So as an example I have legacy page at /crafts/fall-crafts.asp and this works fine - because there is an .asp page sitting there named that. But when i reference /crafts/ .Net takes over and serves the /crafts.cshtml page instead of serving the /crafts/default.asp file. Is there something I can do to keep the power of .Net routing in place, but still have IIS serve the default document in this case?
Modify your RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("crafts/");
...
See RouteCollection.Ignore for documentation.
Similar question (mentions some other possibly needed workarounds): Ignore folder in ASP .NET MVC
The default "routing" built into the Web Pages framework relies on matching incoming URLs to physical files on disk, looking first for the existence of cshtml or vbhtml files. All the time you have a crafts.cshtml file at the same level as a folder called crafts, the file will be served first. The solution is to remove or rename you crafts.cshtml file, but currently that will mess up your URLs.
There is a package which offers greater management over routing in Web Pages applications. It's called Routing For WebPages. It allows you to route URLs to any file name you like. You can download that via the Package Manager or Nuget and read this article that I put together that explains how to use it: http://www.mikesdotnetting.com/Article/187/More-Flexible-Routing-For-ASP.NET-Web-Pages.

ASP.net help rewriting simple URL

<rewrite url="~/Blog" to="~/Blog.aspx" processing="stop"/>
This doesn't work, it only seems to work if I define a replacement filename as well... How do I make it so that
http://www.mysite.com/Blog
Goes to:
http://www.mysite.com/Blog.aspx
See Scott Guthrie's Post
With IIS 6.0 (Windows XP and Windows Server 2003), you can't do this with the usual configuration. IIS looks for a file extension to route the URL to determine which of the installed engines (classic ASP, ASP.Net, PHP, etc.) the request should get routed to. When there's no extension, IIS looks in the corresponding folder (virtual or real) for a default document, like default.aspx or index.htm, etc.
With IIS 7, you can use Integrated mode to get the behavior you want. With IIS 6, you can still do it by specially configuring it to route all URLs to ASP.Net, regardless of extension. For efficiency, you may want to refine it so that static files like images are not routed to ASP.Net. How to do this is explained here: http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/5c5ae5e0-f4f9-44b0-a743-f4c3a5ff68ec.mspx?mfr=true
Consider using MVC routing. If you're using .NET 4 it's even easier. All you have to do is reference System.Web.Routing and then in the global.asax file you can do routes.MapPageRoute("Blog Route", "Blog", "~/Blog.aspx");
Helpful references:
http://msdn.microsoft.com/en-us/library/cc668201.aspx (.net 4)
https://web.archive.org/web/20211020111718/https://www.4guysfromrolla.com/articles/012710-1.aspx (.net 4)
https://web.archive.org/web/20201205221404/https://www.4guysfromrolla.com/articles/051309-1.aspx (.net 3.5)

Using a custom URL rewriter, IIS6, and urls with .htm, .html, etc

I have a custom site I'm building with automatic url rewriting using a custom engine. The rewriting works fine as long as the page url doesn't end in somehting like .htm or .html. For these pages it goes directly to the iis 404 page instead of hitting my rewriting engine first.
I have the * wildcard handler in the "Home Directory" section of the IIS6 configuration of that website but these urls seem to be ignored by it (altho things like css, jpg, js, etc to get sent to the url handler in my web project). How do i set up IIS6 to force these urls to get sent to the handler, while still serving the page if it exists normally?
The handler basically does this
if (!File.Exists(Request.Path))
{
doMyRewriting();
}
I have to assume that using a block like this (just and example, the real one does some other stuff to format the Request.Path to be proper with everything) should run the "doMyRewriting()" if the requested file does not exist otherwise it will serve the page normally. Am I mistaken?
If I tell IIS specifically to send .htm and .html pages thru to the .NET handler the rewriting works but if the page is actually there it will not serve it.
Any help would be greatly appreciated.
Thanks in advance!
Don't know if you can or would want to do this, but there is the Ionics Isapi url rewriter you can use.
http://www.codeplex.com/IIRF
Basically install that then set a rule to remove the .html that way it hits your rewrite engine. I use it on IIS 6 with several of my blogs.
I think if you are having IIS send all requests to .NET and your handler, then your handler will need to detect if the page exists and serve it instead of rewriting.
UrlRewriting.NET has an option to do this - you might want to look at their code to see how they're handling this case.
In my opinion, rewriting URLs with IIS 6 is best handled with an ISAPI filter written as unmanaged native code. Otherwise, you run into the issues you've mentioned - having to map all extensions to ASP.Net and losing the ability for simple file handling. With an ISAPI filter, you can choose to not rewrite some URLs and let IIS handle them as normal.
To get started, I suggest reading the ISAPI Filter Overview on MSDN.
If your filter absolutely needs the .Net framework runtime, it is possible to write a small ISAPI filter shell that hosts the CLR and forwards the requests to some managed code. The Filter.Net Framework takes this approach and may be suitable for your needs. There is the small drawback to this approach in that you will have to use the same .Net version as any ASP.Net applications that are run in the main IIS process.

Categories

Resources