0

I have the following URL:

http://localhost/FolderB/Index

This goes to the following controller:

/FolderB/HomeController.cs

I need to instead use this URL: http://localhost/SubComponent.

I'm not sure how that is done in asp.net MVC. What is this type of masking called and where does the code live to accomplish it?

I have tried the following in RouteConfig.cs but it doesn't work:

routes.MapRoute(
  name: "SubComponent",
  url: "SubComponent",
  defaults: new { controller="Home", action="Index", id=UrlParameter.Optional},
  namespaces: new[] { "MyNamespace" }
2
  • What version of MVC are you using? If you are using MVC 5+ you can use the RouteAttribute to just add another route for SubComponent. Commented Sep 11, 2015 at 18:28
  • I'm using .NET 4.5.1. So which ever version that uses. Commented Sep 11, 2015 at 18:30

3 Answers 3

3

A simple way of doing this would be to add another route in MVC that routes SubComponent.

You can add a route by looking for where your routes are wired up, which is usually either in Global.asax.cs or in a class called RouteConfig and adding your own route:

routes.MapRoute(name: "SubComponent", url: "SubComponent/{id}", defaults: new
{
    controller = "Home",
    action = "Index",
    id = UrlParameter.Optional
});

The order which routes are added matters, so make sure this call to MapRoute occurs before all others.

And specify an area if you need to.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice - ordering of MapRoutes was my issue.
1

For ASP.NET MVC 5, use RouteAttribute. For older ASP.NET MVC version, you may have to resort to other open source (or your own custom) implementations e.g. https://github.com/mccalltd/AttributeRouting

1 Comment

Not sure how this works with RouteAttribute. Shouldn't I be using RouteConfig?
1

For Latest Versions: https://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx

If you are using ASP.NET 3.5 ASP.NET Routing could be a good choice for you.

MSDN page: msdn.microsoft.com/en-us/library/cc668201.aspx

Using it with ASP.NET MVC at ScottGu blog: weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

Using it with ASP.NET 3.5: www.techbubbles.com/aspnet/aspnet-35-url-routing/

If your website runs under ASP.NET 2.0 Helicon ISAPI Rewrite could be a good choise for you. This is an IIS filter that redirects requests to your pages according to regex-based configuration file. They have a free version for one website.

Have a look at Helicon: www.isapirewrite.com

Hope this helps. Good Luck

2 Comments

The ScottGu link seems to be an older version of MVC. I don't believe all of that is definied in Global.asax.cs anymore.
Yes Space, but i was just referring it because i didn't know what version you were using, but the first link should give you some idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.