ASP.NET WEBAPI add custom route - asp.net

I have the new .NET WebAPI up and running and I am trying to add a new route to my project so that I can see the SettingsController in action. I have added my route to the Global.asax file within the RegisterRoutes method but I receive a 404 error when I try and browse to: sampleUrl:12345/settings/index. I am able to see: sampleUrl:12345/home/index without issue.
Global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Settings",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Settings", action = "Index", id = UrlParameter.Optional }
);
}
Do I need to have an ActionResult Index() in my SettingsController?

Your settings route is redundant. What you are probably missing is the index method in settings controller.

Related

Why doesn't this route work if I change its order in RouteConfig?

When I access this route http://localhost:60015/myroute, it does not work if the order of the route like so:
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(
"myroute",
"myroute/{name}",
new { controller = "myroute", action = "search", name = "" }
);
}
It works if I reverse the order. What does that mean? How does that happen?
This is because The order of the routes that you add to the route table is important. Your new custom route should be added before the existing Default route. If you reversed the order, then the Default route always will get called instead of the custom route.

Invalid url in form rendered by html.beginform

I have an application which is mix of WebForms and MVC.
HomeController is a simple controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
On view - /Views/Home/Index.cshtml, I have used Html.BeginForm to render the form.
#using(Html.BeginForm("Index", "Home", FormMethod.Post, new { #class = "form-horizontal"}))
{
#Html.TextBox("username", "");
#Html.Password("password", "");
<button type="submit">Login</button>
}
Routes are configured like below. First 2 routes are using MapPageRoute to map to webforms.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("admin", "admin", "~/admin.aspx");
routes.MapPageRoute("adminEdit", "adminEdit", "~/adminEdit.aspx");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But after my homepage ("Home" is my default controller) is rendered to the browser, form action has url:
/admin?action=Index&controller=Home
not "/Home/" even though i have passed "Index" & "Home" as actionname and controllername to Html.Beginform respectively.
Url routing is working fine when i browse to my homepage. But the form's "action" has invalid url. It seems like it is matching the first route i have configured. Why?
I got it to work by following the instructions in this post:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute(
routeName: "admin",
routeUrl: "{pagename}",
physicalFile: "~/{pagename}.aspx");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
However, I cannot explain why the parameter is necessary to make it not match in this case.

How should I handle MVC and WebAPI routing for a SPA with one index.cshtml page?

I have a single page application that has one MVC file index.cshtml which
is served by the Home controller and Index method. All the logon, logoff
and data requests are served by WebAPI. Can someone confirm if I am setting up
my routing correctly. Here is what I have:
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute("DefaultRedirect",
"",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"catchall",
url: "{*url}",
defaults: new { controller = "Home", action = "Index" });
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: ApiControllerOnly,
routeTemplate: "api/{controller}"
);
config.Routes.MapHttpRoute(
name: ApiControllerAndId,
routeTemplate: "api/{controller}/{id}",
defaults: null, //defaults: new { id = RouteParameter.Optional } //,
constraints: new { id = #"^\d+$" } // id must be all digits
);
config.Routes.MapHttpRoute(
name: ApiControllerAction,
routeTemplate: "api/{controller}/{action}"
);
config.Routes.MapHttpRoute(
name: ApiControllerActionAndId,
routeTemplate: "api/{controller}/{action}/{id}",
defaults: null, //defaults: new { id = RouteParameter.Optional } //,
constraints: new { id = #"^\d+$" }
);
}
Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
WebApiConfig.CustomizeConfig(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
What I am not sure of is the first file that handles MVC routes. Do I need to have
just one route that makes everything go to Home Controller and Index method.
Also should I in the MVC routing ignore those requests that start with api ?
You should not serve your SPA page as index.cshtml (because that means that it is being compiled to the respective class on the server that will serve the response)
It should be served as index.html and all data that it requires should be fetched by AJAX calls to the server.

ASP.Net MVC 4 Route/Routing performance issue

Performance issue with routing.
The asp.net MVC 4 application takes more time to find the controller itself.
Our project structure for route is area/controller/action/id
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{area}/{controller}/{action}/{id}",
defaults: new { area = "QQQQQQQ", controller = "Home", action = "Index", id = UrlParameter.Optional });
}
unlike the find view this routing takes approximately the same time for all the request(second time , third time and etc)

asp.net mvc 4 controller routing depending on url

Depending on url, I want to route different controllers. So depending on different URLs directed to same DNS server, I want to give my website a different look and feel.
To test this locally I tried:
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(
"localhost",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Contact", id = UrlParameter.Optional }
);
}
}
Wat I wanted was the Contact page to appear, not the default Home Page as the URL was :/localhost:portnr./.
How can I get the first bit of an URL (domain) to decide which controller to route?
Suppose you have the Contact controller and Index Action, and you want to load that on page load,
you can specify as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Contact", action = "Index", id = UrlParameter.Optional }
);

Resources