MVC routing via Global.asax.cs not hitting model - c#

I'm trying to route to my controller, I cant figure out what i'm doing wrong, and its irritating me.
Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "Groups/{0}/{1}",
defaults: new
{
controller = "GroupSections",
action = "Details",
groupName = "",
sectionName = ""
}
);
Controller
public ActionResult Details(string groupName, string sectionName)
{
//code
}

If you see this route examples, your route should be:
routes.MapRoute(
name: "Default",
url: "Groups/{groupName}/{sectionName}",
defaults: new
{
controller = "GroupSections",
action = "Details",
groupName = "",
sectionName = ""
}

void RegisterRoutes(RouteCollection routes) should not be in your asax, it should be in your RouteConfig static class.
RouteConfig.cs
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
//your route config.
}
}
Edit: Also, as noted in Brian Mains answer, your route should resemble:
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Where the names in brackets map to the values in the defaults object.

Related

resource api not found in mvc application

In existing MVC application I have added api support.
Added api controller but when i hit url it shows error -
The resource cannot be found.
I tried url -
https://localhost:44316/api/getdata
https://localhost:44316/Test/api/getdata
TestController.cs
public class TestController : ApiController
{
[HttpGet]
[Route("api/getdata")]
public IEnumerable<string> GetData()
{
return new string[] { "value1", "value2" };
}
}
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
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 }
);
}
}
If this is an existing MVC application, then you need to register your web api routes. You need to add this line of code to Global.asax.cs to register your web api routes in Application_Start method:
GlobalConfiguration.Configure(WebApiConfig.Register);
Once you do that, your URLs should then work.
The Route attribute is in wrong place, it should decorate the Controller class.
Try this:
[Route("api/[controller]")]
public class TestController : ApiController
{
[HttpGet]
public IEnumerable<string> GetData()
{
return new string[] { "value1", "value2" };
}
}
and call Get http://localhost:XXXX/api/test.

How to configure the url with a combination of url segmentation and querystring for web api rest url

(using Web api 1.0)
I have a scenario where my action has multiple parameters like
GetCustomer(int id, string email)
where the url would be specified as
GET
api/Customer/{id}/{email}
But i am looking to configured the url as
api/Customer/{id}?email={email}
So it's a combination if url segmentation with query string. Currently when i am trying to set this i am getting this below error.
The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.
Parameter name: routeUrl
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 }
);
routes.MapRoute(
"Help Area",
"",
new { controller = "Help", action = "Index" }
).DataTokens = new RouteValueDictionary(new { area = "HelpPage" });
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
kindly help me in fixing this thanks in advance
Your configuration needs updating
public static class WebApiConfig
{
public static void Register(HttpConfiguration config) {
config.Routes.MapHttpRoute(
name: "CustomerApi_GetCustomer",
routeTemplate: "api/Customer/{id}",
defaults: new { controller = "Customer" action = "GetCustomer" }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
If you make a request to api/Customer/{id}?email={email}
the binder will automatically match the GetCustomer(int id, string email) with the email parameter from the url

CastleWindsor not working with route in asp.net mvc

I have a WebApplication in ASP.NET MVC using the dependency injection with CastleWindsor but when I add a route attribute, the application returns following error "The controller no found".
My ControllerInstaller
public class ControllerInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Classes.FromThisAssembly()
.BasedOn<IController>()
.LifestyleTransient());
}
}
I have a following controller:
[Route("espetaculos")]
[FrontAuthorize]
public class EventController : Controller
{
#region DependencyInjection
private readonly IApplication applicationWeb;
private readonly IEventBusiness eventBusiness;
private readonly IShowBusiness showBusiness;
private readonly ISession sessionWeb;
private readonly ILog iLog;
public EventController(IEventBusiness eventBusiness, IShowBusiness showBusiness, IApplication applicationWeb, ISession sessionWeb, ILog iLog)
{
this.eventBusiness = eventBusiness;
this.showBusiness = showBusiness;
this.applicationWeb = applicationWeb;
this.sessionWeb = sessionWeb;
this.iLog = iLog;
}
when I access the route "/espetaculos" here is the error
The Castle expects only the full path of controller?
Edit
My RouteConfig class
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
//routes.MapMvcAttributeRoutes();
//AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Plateia.Controllers" }
);
}
}
As I suspected, you apparently haven't enabled attribute routing in MVC. Without doing so, adding the [Route] attribute on your controllers and actions will have no effect.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
// This line is required in order to scan
// for [Route] attribute in your controllers
routes.MapMvcAttributeRoutes();
//AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Plateia.Controllers" }
);
}
}

MVC Routing issue for Areas

I have an Area called Racing. I have set up routes to accept parameters using constraints as below:
Global asax:
protected void Application_Start()
{
//AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
Route.config
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 }
);
AreaRegistration.RegisterAllAreas();
}
}
Racing area Registration
public class RacingAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Racing";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
// this maps to Racing/Meeting/Racecards/2014-01-06 and WORKS!!
context.MapRoute(
name: "Racecard",
url: "Racing/{controller}/{action}/{date}",
defaults: new { controller="Meeting", action = "Racecards", date = UrlParameter.Optional },
constraints: new { date = #"^\d{4}$|^\d{4}-((0?\d)|(1[012]))-(((0?|[12])\d)|3[01])$" }
);
// this maps to Racing/Meeting/View/109 and WORKS!!
context.MapRoute(
"Racing_default",
"Racing/{controller}/{action}/{id}",
defaults: new { controller="Meeting", action = "Hello", id = UrlParameter.Optional }
);
}
}
The above two work for the URL's specified but now I cannot visit for example Racing/Meeting/HelloWorld without having to pass a parameter as Racing/Meeting/HelloWorld/1. Any ideas?
Thanks
Your area registrations need to be done before your Default route.
try moving them to the top of the method
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
AreaRegistration.RegisterAllAreas();
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

Asp.net Web API Attribute Routing 404 Error

I cannot figure out for the life of me why my attribute routing isn't working.
Here is my setup:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Attribute routing
config.MapHttpAttributeRoutes();
// Convention-based routing
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Here is my controller with my routing attributes:
[Route("api/v1.0/orders")]
public class OrdersV1Controller
{
[APIAuthentication(RequireAuthentication = true)]
[HttpGet]
[Route("{id:int}")]
public GetOrderResponse GetOrder(int id)
{
.....
}
}
Here is my global asax file:
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Populate;
}
}
Here is the URL i'm testing which is returning a 404 not found:
http://localhost:60105/api/v1.0/orders/111111
your controller needs to be an API Controller :
public class OrdersV1Controller : ApiController

Categories