MVC4 controller/route not working - c#

my VisitsController:
public ActionResult Index(Visits visits, int? id)
{
....
return View(v);
}
Here is my route config
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 }
);
}
Works: http://localhost:49225/Visits
Does not work: http://localhost:49225/Visits/123
What would the custom route be (and force it to be an integer)?

You need to add a new route for that to work.
Currently, this will work:
/Visits/Index/123
I believe you need to add the following:
routes.MapRoute(
name: "VisitsDefault",
url: "Visits/{id}",
defaults: new { controller = "Visits", action = "Index",
id = UrlParameter.Optional }
);
This is assuming you have a modelbinder for Visits already.

Related

Is it possible to have more than one routing in ASP.NET MVC?

I want to ask if it's possible to have more than one routing within RouteConfig class. The logic I have is as below, what I want to achieve. I have actionLink("Dashboard", "Account".....) already and want to have a unique one that won't conflict with existing one when page is loaded. Please assist me there is a way.
namespace ContentManagementSystem
{
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 already have this working fine
defaults: new { controller = "Dashbaord", action = "_Index", id = UrlParameter.Optional // I want to have separate, but unique route for this controller for actionResult
);
}
}
}
Yes you can have more routes you can :
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Student",
url: "students/{id}",
defaults: new { controller = "Student", action = "Index"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
As shown in the above code, URL pattern for the student route is students/{id}, which specifies that any URL that starts with domainName/students, must be handled by the StudentController.
Notice that we haven't specified {action} in the URL pattern because we want every URL that starts with student should always use Index action of StudentController. We have specified default controller and action to handle any URL request which starts from domainname/students.
You can read more here
Add it to top of default route:
namespace ContentManagementSystem
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("other_route", "other_route/",
defaults: new { controller = "OtherController", action = "OtherAction" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // I already have this working fine
defaults: new { controller = "Dashbaord", action = "_Index", id = UrlParameter.Optional // I want to have seperate, but unique route for this controller for actionResult
);
}
}
}

Map api route to mvc route

I want my FilesController to be accesible from both /files/{action} and /api/files/{action}. I tried
routes.MapRoute(
name: "Files",
url: "api/Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
but it not working. I deleted WebApiConfig, so in App_Start is only MVC RouteConfig, but it doesnt help.
Upd
controller code
public class FilesController : Controller {
public ActionResult Index() {
return new HttpStatusCodeResult(System.Net.HttpStatusCode.OK);
}
}
request urls:
/files/index -> OK (200)
/api/files/index -> Not Found (404)
Upd #2
Complete 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 }
);
routes.MapRoute(
name: "FilesRoute",
url: "Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Files",
url: "api/Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
}
}
I think you can try to add a new route in front of your original route setting.
This setting will first check URL whether is match Files/{action}/{id} , if not the use api/Files/{action}/{id}
routes.MapRoute(
name: "FilesRoute",
url: "Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Files",
url: "api/Files/{action}/{id}",
defaults: new { controller = "Files", id = UrlParameter.Optional }
);
EDIT
The problem is route setting order you need to set the default route to be the last one.
/api/files/index => Otherwise, the setting will find api controller and files action.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "FilesRoute",
url: "Home/{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Files",
url: "api/Home/{action}/{id}",
defaults: new { controller = "Home", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}

set index action default action controller in asp.net mvc

I am trying to make index action is the default action in this controller when i write
http//mydomain/action
call index action > http//mydomain/case-studies
will call index action
my problem when i write the url gives me this message The resource cannot be found.
the url i write it http//mydomain/case-studiesit should be call index action but The resource cannot be found.
This is the controller
[RoutePrefix("case-studies")]
public class case_studiesController : Controller
{
// GET: CaseStudies
[Route("Index")]
public ActionResult Index()
{
return View("/views/case-studies/Index.cshtml");
}
[Route("adaep")]
public ActionResult adaep()
{
return View("/views/case-studies/pagename.cshtml");
}
}
this is my route
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
This answer worked with me to keep home route as default and make default action for every controller i changed this from [Route("Index")] to [Route("")] with the same route
[Route("")]
public ActionResult Index()
{
return View("/views/case-studies/Index.cshtml");
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
That route does that.the definition of the route is: if {controller} is empty use "Home" if {action} is empty use "index" if {id} has value place in the id parameter. for every url that has only the controller name, the request should go the index action
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This is because your MapRoute is incorrect you're MapRoute is having a default controller of Home:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
change it to:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "case-studies", action = "Index", id = UrlParameter.Optional }
);

Retrieve QueryString And Force Custom Url To A Page

I am trying to config custom url in the RouteConfig.cs file as follows with the default one:
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(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
}
The above works fine when I write the followings in the url: 1st routing
http://localhost:1234/Product/AddImages/id/1008
Again, the below also works: 2nd routing
http://localhost:1234/Product/AddImages/1008
But I want to force the url to show or write the url in the address bar as the first one for the specific page 'AddImages' like this: http://localhost:1234/Product/AddImages/id/1008
Is there any way to do it I mean keeping the default routing and for a specific page, the first routing option?
Again, I tried to retrieve the QueryString from the first url as follows:
int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); //This works for the second routing option
It says - Input string wasn't in a correct format. I guess, this is for the default routing option as it's configured to:
url: "{controller}/{action}/{id}"
Again, is it possible to get the QueryString value from the below url or to config anything:
http://localhost:1234/Product/AddImages/id/1008
In routing table for MVC, the route that are defined first has the highest priority. So all you need is make sure your specific route higher priority than the default one. Just define it earlier. That is it - as mentioned in the comment.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
If you want only the first one then try removing the generic one
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/id/{id}", //Custom url route
defaults: new { controller = "Product", action = "AddImages", id = UrlParameter.Optional }
);
}
If you want it only for that controller only try this -
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default2",
url: "Product/{action}/id/{id}", //Custom url route
defaults: new { action = "AddImages", id = UrlParameter.Optional },
constraints: new { controller = "Product" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
There is a better way to get url parameters. Instead of
int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); //
try this -
int id = Convert.ToInt32(Request.Params["id"]); //
But if you defined your action method properly, you don't even need to read it from request. It will automatically be mapped.

Route Issue ASP.net MVC 4 not redirecting to Index Action

When I try to access to index Page on controller if I use this URL format :
mydomain/taux/index
This works and the Index page is opened. But if I use this url format (I don't add the Index action to URL)
mydomain/taux/
I got "404 not found", but normally it should works and redirect me to index page automatically.
How to fix it please ?
Taux Controller :
// GET: /Taux/
public ActionResult Index()
{
var taux = db.TAUX.Include(t => t.CATEGORIE).Include(t => t.GARANTIE);
return View(taux.ToList());
}
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 = "Account", action = "Login", id = UrlParameter.Optional }
);
}
}
This is because your default action is set to Login. You need to set it to Index if you want link mydomain/taux/ to redirect to Indext action
If you want to have the redirect just for this specific controller you can use the following route definitions:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Taux",
url: "Taux/{action}/{id}",
defaults: new { controller = "Taux", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}

Categories

Resources