Custom urls for MVC application in subdirectory - asp.net

I'm not sure the correct way to phrase what I'm trying to accomplish, and I'm definitely not a MVC expert. So, I'm not sure the best way to handle this. I feel like there should be a more elegant, MVC based approach than what I've thought of so far.
I have a site with a physical structure similar to the following:
www.example.com (web forms application)
-red (native folder in the parent web forms application)
-blue (mvc subapplication)
So, by default, urls would be like the following:
www.example.com/red/test.aspx
www.example.com/blue/controller/action
For some controllers, I need
www.example.com/blue/controller/action to instead have a publicly visible url at
www.example.com/green/controller/action
I have so far accomplished this with a rewrite rule in the parent/root website. However, the mvc subapplication doesn't natively understand that some requests live under a pseudo folder. So, when I render a form with html.BeginForm for pages requested at www.example.com/green/controller/action, it renders the form's action as /blue/controller/action. Other than outbound rewrite rules, is there someway to "teach" the mvc subapplication about a new root path for select controllers? Is there someway to configure some of this when registering routes on startup? Any other suggestions on the best way to handle this? Now, keep in mind, the rewrite from blue to green is not the entire subapplication. Some controllers will need to be rewritten to have a root of orange, etc.
Obviously, the use of colors is just an example. :-)

Areas in MVC would make things easier and more organized.
Refer to this for more info
Here is another useful link

Related

Single page app in AngularJS and ASP.Net works fine, but when I refresh my page in the browser I get 404 errors

So I've set up an HTML5 single page application, and it's working well. The page is at /App/ and when some one goes to content it looks like /App/Content/1234.
One problem: If the user refreshes the page the server can't find that URL because it doesn't actually exist. If I send them to /App/#/Content/1234, they're golden, but what is the best way to do this? I have a LOT of different styles of URL under /App.
What is the best way to globally catch any request under ~/App/(.*) and redirect it to ~/App/#/$1?
The only route registered in MVC is the standard OOTB route.
Sounds like your server is not re-writing the urls to the app's base URL.
The URL re-writing needed on the web server is server-dependent. For Apache, you'd use mod_rewrite.
Instead, switch Angular to the "Hashbang mode" (the default) so the urls will all store the local state after the # in the url.
I don't want my apps to require server configuration changes, so I recommend hashbang mode.
See AngularJS docs. See section "Hashbang and HTML5 Modes" The HTML5 mode section describes all the configuration issues needed to support HTML5 mode for the urls.
This awesome dude describes how to fix this here.
In brief:
Remove MVC nugets (unless you use MVC controllers for anything) -
you can keep the Web API nugets. Keep WebPages and Razor packages.
Also delete MVC controllers and views.
You can keep using .cshtml
files with some web.config modifications. You'll need this for
bundling.
Finally you add a rewrite rule on web.config to point all urls (excluding content, images, scripts etc) to index.html

Many large projects, routing for image server asp.NET mvc

I work in a company that has over 70 asp.net mvc projects (very similar ones)
All images are linked like this : Url.Content("~/imagefoldername/picname.jpg");
Now I want to merge those 70 projects into one, very similar to Drupals multiple-site per installation system.
I'd like to have all images on one server (maybe with the possibility of even using a CDN)
I'm wondering if it's possible to somehow Route /imagefoldername/picname.jpg to something like
http://img.mycdnname.com/project1/picname.jpg
This way I don't need to iterate trough all views int the 70 projects, but just add a rule to global.asax, and would save me a lot of time.
Is it possible? How do I route it?
You could use IIS URL Rewriting module or just setup separate route in MVC and create an action that will return HTTP 302.
Make sure that you read this article by Phil Haack first.

Can MVC handle regular URL path requests, too?

ASP.NET MVC newbie question:
I've set up a MVC site, with a few controllers. Now my site also has a lot of content files, which are stored in a network of subfolders within my web site, and I need to be able to access them directly, e.g.
http://mydomain.com/Content/Images/Geography/Asia/Japan/TokyoAtNight.jpg
Is there a way to make this a direct pass-through to the content folder, as specified by the path, or do I have to make a Content controller that interprets the rest of the URL and returns the file as some kind of ActionResult? Bear in mind, of course, that there will be lots of different content types, not just JPEGs.
Thanks for your help!
This should work without you doing anything - static files are not processed by the routing engine.
You want to look into Routing, and IgnoreRoute specifically. Here are a couple of places to start.
Asp.Net Routing: How do I ignore multiple wildcard routes?
http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs
Take a look at the #Url.Content() helper method.
Url.Content("~Content/Images/Geography/Asia/Japan/TokyoAtNight.jpg")
Yes.
The IRouteHandler and the route registration in your global.asax is your extensibility point for configuring how MVC handles url paths.
However, by default ASP.NET MVC will allow you to access image files directly, without any additional configuration.

Use asp.net routing on top of old asp system

I'm experimenting with using MVC routing as a temporary fix to get SEO friendly urls on an old (VB6/ASP classic) system while it's being re-written (which will take a long time).
The old system has 1 asp file with a vb6 dll that generates html which is served by a response.write in the master.asp.
so urls on that system look like this:
www.foo.com/master.asp?sessionid=abc123&pagetype=Item&ItemID=My-widget
I'm wondering if I can use an MVC project to create a route for cleaner urls
and have a controller map the values and build the corresponding old url and then do a Server.Transfer to it.
So the new url would look like:
www.foo.com/Item/My-widget
and map to the old url at
www.foo.com/master.asp?sessionid=abc123&pagetype=Item&ItemID=My-widget
both could then be used interchangeably so the existing site doesn;t have to change, but I could use the new cleaner url on external sites for better SEO
Is this possible?
Is there another way to do this?
edit:
since it's not possible to use server.transfer from MVC, I'm now considering using routing in an ASP.net webforms app.
This should allow me to get the routing part of the application done. Will post back here once I've tried it.
I would suggest you use UrlRewriter.net library instead. It has a lot more features than the the built-in Routing framework (including Regex support), support for permanent redirects, and it's all configurable in the web.config file.
I've tried to use Routing before for this sort of thing but found that it became quite limiting very quickly.
http://urlrewriter.net/
Edit: you will still need a .net web project "wrapper" for your classic asp solution though, as you describe in your question, which of course comes with it's own problems as outlined in the other answers.
I cant think of a way that you could do this, but you might have some luck with the url rewriter module in iis: http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/
I think this could be made to work, but it doesn't sound ideal. I suspect you'd run into some issues with sharing data between the two sessions too. It may be a good idea IF you are planning to migrate to .net, and the app will be running in a "half and half" state for a while (if so I'd suggest introducing a managed core library shared between both sides before worrying about url rewrites).
I haven't done much work with classic ASP lately, but I think this post on URL Rewriting in Classic ASP might be helpful to you.

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