Aniket
Aniket

Reputation: 21

I want to call controller's Default action.

Example : My User will enter www.xyz.com/Promo/PROMO123

where "PROMO123" is value, which i require.

above code produces error :

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

However www.xyz.com/Promo/Index/PROMO123 will work properly, but i dont want this.

How can i archive this

 www.xyz.com/Promo/PROMO123

Upvotes: 2

Views: 109

Answers (2)

Ashwini Verma
Ashwini Verma

Reputation: 7525

Have you tried Routing?

Such as

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //Don't forget to add this before default one.
        routes.MapRoute(
            name: "PromoRoute",
            url: "{controller}/{myString}",
            defaults: new { controller = "Promo", action = "Index", myString = UrlParameter.Optional }
        );

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

Upvotes: 1

mrjoltcola
mrjoltcola

Reputation: 20862

You need to define a route pattern for this.

If you want this to work application wide, then you will need to change the default route. But I would suggest simply adding a specific route for this controller, in addition to the default route, because you probably don't want to override the default MVC routing for the whole app or you will lose the ability to use multiple actions per controller.

See your RouteConfig, try this route (MVC pre-5):

 routes.MapRoute("myRoute", "PromoRoute/{id}",
                     new {controller="PromoRoute", action = "Index"});

With MVC5 you can add this directly to your action assuming you've enabled attribute routes:

 [Route("PromoRoute/{id}")]
 public ActionResult Index(string id) {

 }

Upvotes: 0

Related Questions