yBother
yBother

Reputation: 718

"Re-routing" url requests in controller

I have a MVC web app which presents various articles. The url vor the view is always the same (and needs to be):

[...]/ArticleView/ArticleView?id=[some guid]

By providing more and more different articles, I need to create more specific views which also need different controller actions for preparing them. I'd like to create an interface that "dispatches" those controller actions depending on some operations with the guid key of the url.

I don't want to use the interface in the ArticleView Controller itself but somehow re-route the request [...]/ArticleView/ArticleView?id=[some guid] to another action which makes use of this interface then.

Example: Browser requests [...]/ArticleView/ArticleView?id=[some guid] ->internally routed to an Action ResolveView which redirects to

`[...]/SuperSpecialArticleView/ArticleView?id=[some guid]`

Is there any possibility to do this?

Upvotes: 1

Views: 81

Answers (1)

zolty13
zolty13

Reputation: 2231

In ArticleView action you can use kind of service which will decide to reroute to another controller action and return proper View.

Also you can change default Routing in ASP.NET platform: You need to implement your own class inheriting from RouteBase class. You need to implement methods:

public override RouteData GetRouteData(HttpContextBase context)
    ... 

This method matches incoming requests.

public override VirtualPathData getVirtualPath(RequestContext rcontext)
     ....

This method matches outcoming requests.

At the end you can register your routes with your own RouteBase based class.

http://www.dotnetcurry.com/aspnet-mvc/970/aspnet-mvc-custom-routes

Upvotes: 1

Related Questions