How to use URL routing with multiple environments? - c#

I have three different environments that I need to be able to use url routing with:
Debug
Live
Demo
The home pages for these three are:
http://localhost:48060/Login.aspx
http://192.168.0.145/Live/Login.aspx
http://www.website.com/Demo/Login.aspx
Both Live and Demo sit in the same Default Web Site as web applications (live is exposed only internally, while demo is exposed externally).
I want to map these to
http://localhost:48060/login
http://192.168.0.145/Live/login
http://www.website.com/Demo/login
Without triplicating every route mapping, what is the recommended approach?
Thanks!
Example of how I add the route for Debug env:
routes.MapPageRoute("Login", "login", "~/Views/Login.aspx");
More info:
When I tried adding
routes.MapPageRoute("Login", "login", "~/Live/Views/Login.aspx");
routes.MapPageRoute("Login", "login", "~/Demo/Views/Login.aspx");
the routes didn't work. I received a 404 error when trying visit http://192.168.0.145/Live/login Not sure what the problem is.
I'm using IIS 7.1 for published versions and whatever Win XP pro uses for debug.

Youre "environments" seem to be sub-directories of the root of the application. Because the first of your three URLs does not contain a second value (e.g. "http://localhost:48060/debug/login") it's not going to be easy to define one route for all three.
If these secondary environments are defined as their own applications then you should be able to use the same route in each, but we would need more details to help you further.
Please describe your situation a little better and I will update my answer with more information.

Turned out to be a configuration issue that m$ forgot to mention. Got it working by modifying my web.config to use runAllManagedModulesForAllRequests
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">

Related

Directory in MVC3 Project

I have a "Tools" area of the MVC3 site I'm currently working on. One of the tools I'm integrating on the site I need to run in a virtual directory. Setting up a virtual directory under the /Tools folder works fine for the app itself, but for navigation to /Tools/, I'm getting "Directory listing not allowed". How do I tell IIS to let MVC routes handle this URL?
We'll be using IIS6 in production, so it's important for it to work with that.
Thanks in advance.
EDIT: For clarity, here's the setup:
/Tools/RoutedTool1
/Tools/ToolInVirtualDirectory
/Tools/RoutedTool2
/Tools/
The routes for the routed tools work fine, but since I had to create a directory under the root to setup the "ToolInVirtualDirectory", IIS is hijacking the "/Tools/" request and trying to send it to the directory, ignoring the route.
You should set routes.RouteExistingFiles = true in Global.asax.cs.
Remember that since this property plays at global level you have to ignore the css files and other stuff you don't need to handle by the routing infrstructure before setting this. For more idea please refer this post.
In your RegisterRoutes section in Global.asax, you add ignore routes that the routing system ignores. There should already be an example for .axd's in the config.

.NET - Dynamically get Web Root URL of another project

I have 4 WebForms projects in my solution. I want to be able to do URL redirects to a page in another WebForms project.
Currently I have to set a URL/port number in my project web settings, set that URL in my web config as an app setting, then read it and handle redirecting.
This is a real pain and makes deployments to various environments obnoxious. Is there a way to dynamically handle this via code?
To make the above even more complication I may have all projects mapped in IIS as such:
www.mydomain.com/project1
www.mydomain.com/test/project2
www.mydomain.com/test/project3
But, that shouldn't matter because you can do this to get the Web Root Url for the server that I want to redirect to:
HttpContext.Current.Request.ApplicationPath;
I am not sure if just handling the web config route is my best option or if I can do this dynamically?
Thanks for any assistance.
This is a pretty standard pattern here and believe it or not, convention and configuration are the ways to handle this -- IIS can lie, or IIS might not know what you want it to do and can easily give you the wrong answer. So, as I was saying:
Convention: got some sort of standard relationship for urls within your app? Especially with regards to dev, CI and QA which are typically the frequent deployments? If so, setup a default convention there so that your app always thinks the right way. Typically the best or at least easiest to handle is to have all the different apps under IIS applications off a shared root.
Configuration: usually for production use where you might have url/ssl/other things at play. Make all the folders configurable.
Trick to mining the stuff out of IIS, besides the fact it often doesnt give you the answer you want, is that you've got alot of environmental stuff to work through. Presuming your app can get the permissions to query the metabase, you still need to know the idea of the virtual site, etc. It is actually easier to explicitly specify the URL than to figure it out.
http://www.gotknowhow.com/articles/how-to-get-base-url-in-aspnet
public static string BaseSiteUrl
{
get
{
HttpContext context = HttpContext.Current;
string baseUrl = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/') + '/';
return baseUrl;
}
}

URL with no query parameters - How to distinguish

Env: .NET 1.1
I got into this situation. Where I need to give a URL that someone could redirect them to our page. When they redirect they also need to tell us, what message I need to display on the page. Initially I thought of something like this.
http://example.com/a.aspx?reason=100
http://example.com/a.aspx?reason=101
...
http://example.com/a.aspx?reason=115
So when we get this url based on 'reason' we can display different message.
But the problem turns out to be that they can not send any query parameters at all. They want 15 difference URL's since they can't send query params. It doesn't make any sense to me to created 15 pages just to display a message.
Any smart ideas,that have one URL and pass the 'reason' thru some means?
EDIT: Options I'm thinking based on Answers
Try HttpRequest.PathInfo
or Second option I was thinking was to have a httphanlder read
read the path like this - HttpContext.Request.Path
based on path act. Ofcourse I will have some 15 entries like this in web.config.
<add verb="*" path="reason1.ashx" type="WebApplication1.Class1, WebApplication1" />
<add verb="*" path="reason2.ashx" type="WebApplication1.Class1, WebApplication1" />
Does that look clean?
Thoughts:
Path Info: http://msdn.microsoft.com/en-us/library/system.web.httprequest.pathinfo.aspx
urls would be http://example.com/a.aspx/reason100, http://example.com/a.aspx/reason101, etc
URL Rewriting : http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
urls would be http://example.com/a/reason/100.aspx, http://example.com/a/reason/100.aspx, etc.
edit: both these approaches involve only one aspx page, but multiple urls pointing to it.
Assuming IIS (I run this on IIS 6 but I expect it would run on 5 as well) you could install IIRF. You could then configure different "friendly" urls a la Apache's mod-rewrite and send them as query params to a single as*x page.
Can they send POST variables?
Too bad you are at 1.1 because the later versions support routing which allows for RESTful URLs.
Another option would to be write a custom HttpModule and intercept the incoming requests.

global.asax not being updated

We have a web application where we are using global.asax for url rewriting. We use a compiled version of the site on the live server.
As a part of modification request, we had to add some custom native AJAX code where javascript would call a webservice to update the content of the page. For being able to call the webservice with extension .asmx, we modified the url rewriting code to handle asmx requests seperately.
this arrangement works fine on the local machine, but when we publish the site and deploy it on the live server, the new code doesnt seem to get included. It still skips the condition to check the ".asmx" extension, and throws a page not found exception considering the webservice name as a page name.
We have tried looking all over and googled for such things as well.. but no avail..
any pointers on what might be going wrong.. ?
Assuming your URL rewriting is good (isn't that normally implemented as a HttpModule?) I'd check to make sure that there's an ISAPI mapping in IIS on production that sends .asmx requests to ASP.NET.
If you think your changes to the global.asax haven't been rejitted then you can always stop the application pool, go find your web applications compiled bits in c:\windows\microsoft.net\framework[version]\temporary asp.net files... and delete the jitted version. I've seen ASP.NET miss when it comes to Jitting changes before.
I'd also consider running http fiddler (IE) or tamper data (FireFox extension) on one of the pages that makes calls to the web service. It will tell you exactly how the page is calling the web service and you can validate that the called URL is correct.
There is machine.config file where you can add HttpModules. I also think that you can do that through web.config.
One reason I can think of is that in the Web.config, you might have configured the routing module in the system.web section but not in system.webServer (or at least forgot something in there).
I the similar problem before and the solution was to remove the module & add it again in the system.webServer section of the Web.config like this:
<system.webServer>
<modules>
<remove name="UrlRoutingModule" />
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, e="RoleManager" type="System.Web.Security.RoleManagerModule"/>
</modules>
</system.webServer>
It might be a different module or handler but the idea is basically the same. It's important to "remove" the module first.

Rewriting URLs in ASP.NET?

I am using ASP.NET C#.
How do I implement URL re-writing procedure that is similar to StackOverflow.com?
http://stackoverflow.com/questions/358630/how-to-search-date-in-sql
Also, what is the meaning of values such as "358630" in the URL? Is this the question ID (the basis for which they use to fetch the data from the table)? Whatever it is, in my application I am identifying records using an "ID" field. This field is an identity column in an SQL table. Right now, my URLs are like the following:
http://myweb.com/showdetails.aspx?id=9872
But I'd like them to appear like:
http://myweb.com/showdetails/9872/my_question_title
Or:
http://myweb.com/9872/my_question_title
Or whatever the best way, which will taste good to search bots.
My application is hosted on Go Daddy's shared hosting service, and I feel that no customized ASP.NET "HTTP module" or no customized DLL for URL re-writing is working on their server. I tried many samples but no luck yet!
I found that Stack Overflow is hosted on Go Daddy (shared hosting?). Maybe Stack Overflow's method will work for me.
SO is using ASP.NET MVC. You really need to read in details how MVC URL rewriting works, but the gist of it is that the 'questions' part in the URL is the name of the Controller class (which roughly corresponds to the 'showdetails' in your URL) and the number is a ID parameter for the default action on that Controller (same as the parameter 'id' in your URL).
Since MVC isn't an option you can try redirecting the 404s. This will work in ASP.NET 1.1 and above: Redirect 404s and 405s to your own handler using either IIS config or web.config, parse out the request in the handler and redirect to the appropriate resource.
<configuration>
<system.web>
<customErrors mode="On" defaultRedirect="error.html">
<error statusCode="404" redirect="newHandler.aspx"/>
</customErrors>
</system.web>
</configuration>
Before the advent of System.Web.Routing, the common practice was to use UrlRewriter.NET. Worked well enough, but could bite you when configuring IIS. I'm not sure if there are any simple ways of using the new Routing classes in ASP.NET (i.e., drop it in and go vs. refactoring code).
please explain the meaning of values
such as "358630" in the URL
That is (presumably) the ID for the question in the database. In the MVC model
myurl.com/questions/358630
is analogous to
myurl.com/questions.aspx?id=358630
The question title on the end of the URL is actually being ignored by the app. It's generally "tacked on" for search engine optimization and human readability purposes. In fact, you can change the title of this question in the URL and notice the page still loads just fine.
The new System.Web.Routing dll is part of ASP.NET 3.5 SP1, and is bin deployable on ASP.NET 3.5, so you could use the features of that on a classic ASP.NET WebForms site.
You'll probably want to take note of Phil Haack's comments in his post on using MVC on IIS 6 as you'll probably need to include the .aspx extension in your routed urls
http://www.mysite.com/controler.aspx/action/id
You might also want to check out Questions Tagged SEO.
The ignored question name at the end of the url is often called a "Slug", and is used for SEO purposes to include the page title in the url.

Categories