ASP.NET MVC map single controller to root of website - c#

I am working on an ASP.NET MVC 5 website that has a public "marketing" website that contains somewhat static pages with information about the company, legal, social, contact us, etc. that a non-logged in user can access. Then, once logged in, there is a back end of the website that registered users have access to features.
Originally I had all the public "marketing" pages going to http://www.mywebsite.com/Marketing/About, http://www.mywebsite.com/Marketing/Social, etc.
However, the business wants all the "marketing" pages, to be accessible a single directory down from the root website, so the links above would be accessible with:
http://www.mywebsite.com/About, http://www.mywebsite.com/Social, etc.
I know I can use the below approach to get it to work by registering individual routes for each "marketing" page like so:
routes.MapRoute(
"ShortAbout",
"About",
new { controller = "Marketing", action = "About" }
);
routes.MapRoute(
"ShortSocial",
"Social",
new { controller = "Marketing", action = "Social" }
);
However, since there are about 15 "marketing" pages, this seems inefficient and it seems like there must be a better way to do this.
I also tried a generic routing approach outlined here: http://www.wduffy.co.uk/blog/aspnet-mvc-root-urls-with-generic-routing/
but the problem with that approach was I had a "marketing" page, with the same name as a controller and it ended up forwarding the user to the marketing subdirectory. For example, I had a Controller called "MachineController", and in the "MarketingController" I had an action/page called "Machine", so it was forwarding the user to /Marketing/Machine using the approach in the above link.
Any other ideas? Thanks in advance.

I had exactly this problem. A much simpler but more hardcoded solution is
routes.MapRoute("MarketingPages", "{action}",
new { controller = "Marketing" },
new { action = #"About|Social" });
The last anonymous object constrains the route to match routes where action matches the supplied regular expression, which is simply a list of the urls you want to have marketing pages. Any other url like '/something' falls through to the routes below.

Related

MVC manipulate URL (routing), is it possible?

I have a website that use this pattern.
http://www.domain.com/product/...
My question is now, i need to create a subsite that going to be with this URL pattern, i have tried to change the routing without success.
http://www.domain.com/companyname/product/...
How can i inject the companyname in the URL without breaking my current routing?
Thanks
Niden
Three ways:
If it's relatively static, you can follow Andy's advice in the comments and publish the site in a virtual directory, companyname. Assuming you've properly used the UrlHelper extensions to generate URLs, instead of just hard-coding paths, then everything will just work.
You can create a "companyname" area. The default routing for an area is /area/controller/action. So that would get you the URL structure you want. However, areas are somewhat segregated, so you would need to copy controllers and views to the area's directory. Although, you could subclass controllers from the main app in the area to reuse code.
Just change the default route/add a new route:
routes.MapRoute(
"CompanyDefault",
"{company}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
// default route here

ASP.NET MVC5 - append querystring to all URLs

My team is building a simple MVC site for very low end "feature" phones. One problem we have is that certain phone aggressively cache HTML, so what the user actually gets to see is not what we intend. An extreme example is, a user registers and gets a "thank you page". Another user on the same device then tries to register. The phone simply serves the cached page without creating a new account.
In the past I've dealt with this by adding a "cache buster" querystring to things I don't want cached, eg all pages will be served in the format
http://domain.com/controller/route?cb=somerandomstringofnumbers
In this case we'd need to do this for all URLs in the site - this includes the URLs auto-generated by Controller actions such as RedirectToAction or Redirect and also the Razor Url.Action, Html.BeginForm, Html.ActionLink etc.
Now obviously I could decorate the Razor HTML helpers (or extend them) and add the argument to an controller action, but it seems to me that because the actual URLs generated by these built in methods are auto-generated from the Controller/Action params passed in, there should be a way to hijack that process.
Unfortunately the MS classes are protected - I'm mostly looking in System.Web.Routing.Routes.
I've tried a few things I've found online but they are not MVC5 (dating back to 2008) and it seems the framework has changed significantly.
eg, from
http://forums.asp.net/t/1216840.aspx?Append+value+to+all+urls+built+by+RouteCollection+GetUrl
public class SessionAppendingRouteHandler : IRouteHandler
{
public IHttpHandler GetHandler(RequestContext context)
{
SessionAppendingHttpHandler handler = new SessionAppendingHttpHandler();
handler.RequestContext = context;
return handler;
}
}
public class SessionAppendingHttpHandler : MvcHandler
{
public override ProcessRequest(RequestContext context)
{
//append your sid here
}
}
// and in the route setup
RouteTable.Routes.Add( new Route
{
Url = "/[controller].mvc/[action]/",
Defaults = new { action = "index" },
RouteHandler = typeof(SessionAppendingRouteHandler)
});
This I cant get to work as the framework has changed too much, but it looks very close to what I would like to achieve.
I feel like I'm in the right area, but I've hit a brick wall.
Any suggestions?
This is quite old, but let me answer based on how I solved a similar problem:
Instead of having it as query string, have the cb as a route value just as action and controller are route values. You can do this by registering a route; for instance:
routes.MapRoute(
name: "CB",
url: "{cb}/{controller}/{action}/{id}",
defaults: new { cb = "3600", area = "", id = UrlParameter.Optional }
);
If the value for cb is not a constant, then you can find a convenient point to set the cb for each user session. A good place will be after a successful login. With this, you'll now need to provide just two custom methods for RedirectToAction and ActionLink. Your implementation will simply package a RouteValueDictionary and then pass it in to MVC's own implementation using the appropriate overloads.

Deploying MVC Area as a self standing web site

I have an MVC Application with multiple Areas. They share a lot of common code and components, so I do not want to break them up into separate Projects. But I would like to deploy them to separate web sites.
The normal routing is:
www.mysharedsite.com/Area1
www.mysharedsite.com/Area2
...
But I would like to deploy them as:
www.area1site.com/
www.area2site.com/
...
I was thinking of putting a field in the web.config and then adding logic in the RouteConfig and the RegisterAreas of each area to change the Routes and turn off Routes to Controllers altogether. But this seems kludgy.
Is there a clean way of doing this?
What I would do is create and install a custom ActionInvoker which reads the hostname from the request, and based on it, sets the appropriate Area path for you:
protected override ActionResult InvokeActionMethod(...)
{
// Get hostname
var hostname = controllerContext.HttpContext.Request.Url.Host;
if (hostname == "some value you want")
{
controllerContext.RouteData.DataTokens["area"] = "your area here";
}
return base.InvokeActionMethod(controllerContext, actionDescriptor, parameters);
}
You could specify a route based on the hostname, mapping it to an area. Based on the URL format in your question:
routes.Add("DomainRoute", new DomainRoute(
"{area}site.com", // Domain with parameters
"{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
));
See this post for the DomainRoute class:
http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx
Why not put the common code in a seperate dll
and link your websites to this dll?
Your solution will be a lot bigger if you add another website that also shares the common code.

Allow user when registered to browse to username.host.com

I am using Asp.net MVC3 and C# and IIS 7.5. I want that once user is registered he can browse my site using username.host.com and this username should be available to me in my query string so I can show the data related to that particular username only. All the logic is the same for all users. I don't want to do any fancy thing like if user1.host.com is entered then I want to redirect to a separate controller and action etc. All the application logic is the same for all users. I just want to change the way the url is shown in the browser.
Note: I am not talking about really creating dynamic subdomains. That is a lot of task!
Because routing is so powerful in MVC, I assume that it can be done alone using routing. Also, if possible I want this to work on localhost also in IIS/Cassini.
Eg: If I browse to jaggu.localhost:19883. It should send me to localhost:19883/Home/index/Jaggu (because by default Home is the controller and index is the method)
I am completely clueless on how to achieve this. Any help would be appreciated.
Thanks.
In terms of ASP.NET MVC routing it's easy. Simply write a custom route:
public class MyRoute : Route
{
public MyRoute(string url, object defaults)
: base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
{ }
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var rd = base.GetRouteData(httpContext);
var tokens = httpContext.Request.Url.Host.Split('.');
if (tokens.Length > 1)
{
rd.Values["username"] = tokens[0];
}
return rd;
}
}
and then register this route:
routes.Add(
"Default",
new MyRoute(
"{controller}/{action}/{username}",
new { controller = "Home", action = "Index", username = UrlParameter.Optional }
)
);
Now when someone requests http://foo.host.com, automatically the Index action of the HomeController will be invoked and passed username="foo" parameter.
Then comes the difficult part. The registration and management of subdomains and web server configuration. A topic better suited to be discussed on http://serverfault.com
It is significantly easier to work with www.{site}.com/{username} than what you are trying to do.
subdomains are meant to section off separate websites; not content areas.
Even if you get it to work, you will discover more issues like dealing with SSL certs (if you need them) and the browser's always fun "same origin policy."

Routing Rule for ASP.NET Products Website

I am building a product catalog for a customer website under ASP.NET using .NET Framework 3.5 SP1. Each product has a part number and an OEM part number (all globally unique).
For SEO purposes I would like the OEM part number to be as close as possible to the actual domain name.
How do I build a routing rule that allows me to do this:
http://www.myDomain.com/oemPartNumber
http://www.myDomain.com/myPartNumber
while still being able to do this:
http://www.myDomain.com/welcome
http://www.myDomain.com/products
http://www.myDomain.com/services
http://www.myDomain.com/contact
I would also love to hear your other SEO suggestions (we are primarily interested in Google) if you have any.
Thanks.
IMPORTANT: This not an MVC site, so I don't have controllers.
You should be able to specify something like http://www.mydomain.com/oempartnumber/oem and http://www.mydomain.com/mypartnumber/pn. There must be something in the url that allows you to choose the controller you want to use and further more allow you to distinguish between a part number and an oem part number (unless those are also unique against one another. If there will never be overlap between oem and pn then you could have http://www.mydomain.com/{partnumber}/pn.
RouteTable.Routes.Add(new Route
{
Url = "[query]/pn",
Defaults = new { controller="PartNumber", action = "Details" },
RouteHandler = typeof(MvcRouteHanderl)
});
You could use some trickery with a route like this:
routes.MapRoute(
"Part number",
"{partNumber}",
new { controller = "Part", action = "Display" },
new
{
partNumber = #"\d+" // part number must be numeric
}
);
But the problem here is that an OEM part number that is not actually a part number (such as "ave-345") would not match!
UPDATE: In reading I noticed that you said "this is not an MVC site so I don't have controllers!"...OH! That changes things. In that case you can check to see if the directory exists where you pass in http://www.mydomain.com/1234 and if not you can test it for a product number. This would have to be done in a HttpModule though so you can catch it before your page is executed. Then on the server side you can direct the page to http://www.domain.com/productdetails?pid=1234.
Take a look here to understand that: http://www.15seconds.com/Issue/020417.htm
For this you will have a class that inherits from IHttpModule. Then you can specify an Init method
public void Init(HttpApplication application)
{
//let's register our event handler
application.PostResolveRequestCache +=
(new EventHandler(this.Application_OnAfterProcess));
}
This then points to your Applicaton_OnAfterProcess method:
private void Application_OnAfterProcess(object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
...
Inside of here you can specify some rules about what you are looking for.
I usually do something along the lines of
if (!System.IO.File.Exists(application.Request.PhysicalPath)) //doesn't exist
{
//you test for your product ID here
...
//if you find it stuff it into a ProductID variable for later...
Once you isolate your product ID you can then rewrite the URL (server side) and direct the user to the proper productDetails.aspx page.
context.RewritePath("~/products/productDetails.aspx?ProductID=" + ProductID.ToString());
So while the user and google sees http://www.mydomain.com/1234 your application will see http://www.mydomain.com/products/productdetails.aspx?productid=1234 and you can code against it as usual.
I hope this is what you were looking for instead!
If there are specific formats to the part numbers you can use regex constraints on the route like this:
routes.MapRoute(
"Part number",
"{partNumber}",
new { controller = "Part", action = "Display" },
new
{
partNumber = #"\d+" // part number must be numeric
}
);
Text like "welcome" won't match the regex and so will ignore this route.
I see that you have already accepted an answer, but let me post a new one that is simpler.
This not an MVC site, so I don't have
controllers.
If you use Web Forms, this is for you: Using Routing with Web Forms
Basically, you have to create your own IRouteHandler and set up your routes to it. Then, you can use it to map as many URLs as you want to your Web Forms. This saves you from the old URL rewriting method and such dirty tricks. (The download link on that blog entry doesn't work anymore, but this is pretty simple to imlement. Anyways, if you need a working example, I can provide you with one.)

Categories