0

I have a website which is functioning in asp.net webform application. Currently we are planning to convert this webform application to MVC with the following conditions.

  1. New modules alone will be developed in MVC pattern.
  2. Existing modules will remain in aspx page. ( no conversion to MVC).
  3. Home page will be (default.aspx)

for example www.example.com will point to www.example.com/default.aspx

new module which will be developed will have the following parameters.

 www.example.com/place/Japan
 www.example.com/place/USA
 www.example.com/place/(anycountry)

so I developed a controller named place.

code:

  public class PlaceController : Controller
  {
    //
    // GET: /Place/
      [HttpGet]
    public ActionResult Index()
    {

        return View();
    }

   public ActionResult Index(string country)
      {

          return View();
      }

when I type in the following URL : http://example.com/ It goes to http://example.com/default.aspx

my routeconfig has:

 routes.MapRoute("Default", "{controller}/{action}/{id}", new { id = UrlParameter.Optional });

but when i add the following

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

and type in www.example.com it goes to place controller, instead of going to default.aspx page. How will I solve this problem

also if I type in www.example.com/place/japan I get resource not found. How should I be modifying the index actionresult?

Venkat
  • 1,702
  • 2
  • 27
  • 47

1 Answers1

1

You can't have two actions named the name way ("Index") and that both respond to GET requests, because MVC won't know which one to use (edit: unless you decorate one with [HttpGet]. and this will have priority over the other).

So, in your Controller, you need to rename the one with the "country" parameter to "IndexByCountry". and then try this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapRoute(
        name: "Place",
        url: "Place/",
        defaults: new { controller = "Place", action = "Index" }
        );

    routes.MapRoute(
        name: "PlaceByCountry",
        url: "Place/{country}",
        defaults: new { controller = "Place", action = "IndexByCountry", country = "" }
        );
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { id = UrlParameter.Optional });
}
Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130