Configure routes using areas in ASP.NET MVC 5

Asked

Viewed 1,172 times

2

I am trying to set a route using area as follows:

At Routeconfig.Cs

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

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

I created an area called "Adm" and on Admaregistration.Cs is as follows:

public class AdmAreaRegistration : AreaRegistration 
{
    public override string AreaName 
    {
        get 
        {
            return "Adm";
        }
    }
    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "Adm_default1",
            "Adm/{controller}/{action}/{id}-{descricao}",
            new {controller = "EntradaDeUniformes", action = "Exibir"}
        );
        context.MapRoute(
            "Adm_default",
            "Adm/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}

Entradadeuniformescontroller.Cs which is inside the Adm/Controllers folder

    public ActionResult Exibir(int? id, string descricao)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        EntradaDeUniforme entradaDeUniforme = db.EntradaDeUniformes.Find(id);
        if (entradaDeUniforme == null)
        {
            return HttpNotFound();
        }
        return View(entradaDeUniforme);
    }

But when I try to pass:

http://localhost/escola/Adm/EntradaDeUniformes/Exibir/1-Calça-Azul

Returns this error: inserir a descrição da imagem aqui

I’m doing something wrong or missing something?

1 answer

2


How I set the route this way "Adm/{controller}/{action}/{id}-{descricao}"

When I passed by http://localhost/escola/Adm/EntradaDeUniformes/Exibir/1-Calça-Azul

It didn’t work because "Blue" was understood as another parameter because of "-".

So I did it differently in Admaregistration.Cs:

context.MapRoute(
            "Adm_default1",
            "Adm/{controller}/{action}/{id}/{descricao}",
            new {controller = "EntradaDeUniformes", action = "Exibir"}
        );

Passing by:

http://localhost/escola/Adm/EntradaDeUniformes/Exibir/1/Calça-Azul

That works perfectly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.