MVC ActionLink GET not working - asp.net-mvc-4

I have the following link
<i class="fa fa-user-md"></i> Inspections
In my route I have the following
routes.MapRoute("Inspections", "{controller}/{action}/{id}", new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });
In Visual Studio when I click the link it works, on my live server I get a white page with the following in bold
xxx - /inspection/ and also a link [To Parent Directory]
All of my other links in the Application is working fine and as expected
Now I have to go and overide the url in the address bar to go xxx/Inspection/Index
The Index method does also have [HttpGet]
Edit 1:
I tried the following, I also tried removing the route
routes.MapRoute("Inspection", "{controller}/{action}/{id}", new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });
Edit 2:
Here is all the routes I have
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("Login", "{controller}/{action}/{id}", new { controller = "Login", action = "Index", id = UrlParameter.Optional });
routes.MapRoute("Employees", "{controller}/{action}/{id}", new { controller = "Employees", action = "Index", id = UrlParameter.Optional });
//Added the following to try to fix issue
routes.MapRoute("Inspection", "{controller}/{action}/{id}", new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });
Edit 3:
in InspectionController.cs I have the following Index method
// GET: Inspection
[HttpGet]
public ActionResult Index()
{
return View(new ActiveInspectionsViewModel());
}
Edit 4:
Here is the blank page that opens (example)
xxx - /Inspection/xxx - /Inspection/ [To Parent Directory]
Edit 5:
I think were getting closer to the issue, I have disabled Directory Browsing now I am getting the 403 error, which is really weird because all my other Action links works (/Employees, /InspectionTemplates, /Clients, /Emails, /CompanyLevels) It is Just /Inspection that is causing the problem, my url is inspection.xxx.co.za can it be the park domain causing the problem?

Look at your route definitions.
routes.MapRoute("Login", "{controller}/{action}/{id}",
new { controller = "Login", action = "Index", id = UrlParameter.Optional });
routes.MapRoute("Employees", "{controller}/{action}/{id}",
new { controller = "Employees", action = "Index", id = UrlParameter.Optional });
routes.MapRoute("Inspection", "{controller}/{action}/{id}",
new { controller = "Inspection", action = "Index", id = UrlParameter.Optional });
All three definitions you registered has the same url pattern, {controller}/{action}/{id}.
So whenever you request a page, The request url will be matched against the registered route definitions and when it gets the first match, the request will be send to the controller-action specified in the definition.
So when you request Inspection/Index, it is matching to the first definition (Login) and the request will be send to Login/Index action method.
Since you do not have anything other than the custom pattern in all the three routes,you do not need those. Just remove those and keep 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 }
);
}

Related

Issue in GET route in asp.net mvc razor c#

I have 3 routes total in my code.
If I change the position of 1st with 2nd, then 2nd starts to work but 1st gives 404 error.
Am I doing anything wrong in below code?
This is GET route and works perfectly.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new {
controller = "Category",
action = "Index",
Category = UrlParameter.Optional
}
);
This is GET route and always give 404 error.
routes.MapRoute(
name: "Default1",
url: "{Category}",
defaults: new {
controller = "Product",
action = "Index",
Category = UrlParameter.Optional
}
);
This is POST route and works perfectly.
routes.MapRoute(
name: "Default_Without_Action",
url: "{controller}/{action}/{Category}",
defaults: new {
controller = "Product",
action = "GetProducts",
Category = UrlParameter.Optional
}
);
test if you put url: "{controller}" and see if this works
routes.MapRoute(
name: "Default1",
url: "{controller}",
defaults: new {
controller = "Product",
action = "Index",
Category = UrlParameter.Optional
}
);
I checked both routes and working fine.
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Route -1
routes.MapRoute(
name: "Default1",
url: "{Category}",
defaults: new { controller = "Product", action = "Index", Category = UrlParameter.Optional }
);
//Route - 2
routes.MapRoute(
name: "Default",
//url: "{controller}/{action}/{id}",
//defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
url: "{controller}/{action}/{Category}",
defaults: new { controller = "Category", action = "Index", Category = UrlParameter.Optional }
);
}
On initial/first request it matches the route-1 and calls the Index Action Method of Product Controller as shown in the picture below. Reason - We defined the defaults parameter for controller Products.
If we remove the defaults parameter from route 1 then on initial/first request route-2 URL pattern matches and Index Action method of Category Controller will be called, Reason - We defined the defaults parameter for controller Category.
Browser response with 404 error only when any request doesn't match with the registered URL pattern.

Specific routing for a controller

I have this url:
http://localhost:3256/publicview/details?username=mvcpro4
which works and returns the view with data.
which i would like to be this url:
http://localhost:3256/publicview/details/mvcpro4
which doesnt work, when it gets the controller the username parameter is null.
How do i need to configure the publicview route, so it doesn't break the default routing.
I have these routes in place.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"publicview",
"publicview/details",
new { controller = "publicview", action = "details" }
);
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
pretty much just like you have already
routes.MapRoute(
"publicview",
"publicview/details/{userName}",
new { controller = "publicview", action = "details", userName=UrlParamter.Optional }
);
Make sure that you call the string parameter userName in your details action so it populates.
or you can just remove the above route and change the parameter to id in your details action so it uses the standard route
be careful of special characters in the url, or .net runtime will kick off and throw exceptions at you.
http://blogs.iis.net/nazim/archive/2011/04/18/use-of-special-characters-like-in-an-iis-url.aspx

MVC4 Default route when using areas

I'm trying to use areas within MVC app, I would like that the default route will be resolved to the HomeController within the admin area but it resolves to the home controller in root site.
I added the namespace of the admin HomeController but it still resolved to the root HomeController.
My Route config:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] {"MvcApplicationAreas.Areas.Admin.Controllers"}
);
}
}
admin area route
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
HomeController - Admin area
namespace MvcApplicationAreas.Areas.Admin.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Any idea why it wouldn't resolve properly?
Thanks
The most straightforward way is to add a data token to your default route:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens.Add("area", "Admin");
Simply add
.DataTokens.Add("area", "[your area name]");
to the end of your default route definition.
I've tested your code for the area registration and it works and selects the right controller. However, the view resolution finds the view in the root folder, even if the right controller is used.
To test, I used the following home index action in my areas home controller:
public ActionResult Index()
{
return View(model: "Admin area home controller");
}
Then my index.chstml in /Views/Home:
Root View: #Model
and my index.cshtml in /Areas/Admin/Views/Home:
Admin Area view: #Model
When run, the output is:
Root View: Admin area home controller
So the route makes the home controller in the admin area run, but then thew view resolution goes on and finds the root view instead of the admin view.
So in the end, it is indeed the view selection that is wrong, so your problem is the same as in How to set a Default Route (To an Area) in MVC.
This solution will broke your API route.
You must have some unique name for each area, like the default area makes it:
context.MapRoute(
"Common_default",
"**Common**/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
The correct solution to default route below:
Web area route:
context.MapRoute(
"Common_default",
"Common/{culture}/{controller}/{action}/{id}",
new {culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Main route which redirect users to the "Common" area:
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "SMS.Sender.Areas.Common.Controllers" }
).DataTokens.Add("area","Common");
Try doing this way....it will help in distinguish.Even if you dont add area="Web", its fine
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", area="Web", id = UrlParameter.Optional },
new[] { "Nop.Web.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
Same way
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", area = "Admin", id = "" },
new[] { "Nop.Admin.Controllers" }
);
Add the Index action method in your default root home controller. In the Index action method use "return redirecttoaction" statement for the default area redirection
public ActionResult Index()
{
return RedirectToAction("Dashboard", "Home", new {area = "Home"});
}
Default route should be
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {controller = "Home",
action = "Index", id = UrlParameter.Optional}
);
Easiest way to route from areas from AreaRegistration Page:
context.MapRoute("MainDefault", "Main/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } , new[] { "Namespace.To.Controllers" });
context.MapRoute("ClearPath", "", new { controller = "Home", action = "Index" }, new[] { "Namespace.To.Controllers" });
In your
RouteConfig.cs
routes.MapRoute("redirect all other requests", "{*url}",
new {
controller = "UnderConstruction",
action = "Index"
}).DataTokens = new RouteValueDictionary(new { area = "Shop" });
default route is by default route in MVC will call at first because in MVC which route is specified very first it will call like that

setting custom path in routeconfig.cs in mvc 4

Below is my routeconfig.cs file in mvc4 application
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{bizId}",
defaults: new { controller = "Home", action = "Index", bizId = UrlParameter.Optional }
);
routes.MapRoute(
"Outlet",
"Outlet/{bizId}",
new { controller = "Home", action = "Index" },
new { bizId = UrlParameter.Optional }
);
}
When i run the application, i need to enter /Home/Index?bizId=1 or any Id after the localhost port to run my application. It is working fine. But, now as the second route.maproute, I want the url to show as for ex: localhost:49787/Outlet?bizId=1 but this doesnt work. Please help! Thanks in advance
got the fix:
routes.MapRoute(
name: "Outlet",
url: "Outlet/{bizId}",
defaults: new { controller = "Home", action = "Index", bizId = 1 }
);

mvc4 web api 404

can anyone see anything wrong with my route configuation? I'm trying to call my web api controller via:
localhost/Attribution/api/Client
localhost/Attribution/api/Fund
I have a couple of controllers that extend ApiController called ClientController and FundController.
Here is my route configuration. I always get HTTP 404 The resource cannot be found.
GlobalConfiguration.Configuration.Routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "test",
routeTemplate: "api/{controller}/{id}/{action}",
defaults: new
{
id = RouteParameter.Optional,
action = RouteParameter.Optional
});
You need to include an action and that action should be in your controller. As written, the url /api/Client doesn't know what action to call. Instead, try:
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "test",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new
{
action = "Index",
id = RouteParameter.Optional
});
Then the URL /api/Client should call action "Index" in controller "Client".