Cannot determine real path for ASP.NET MVC application - c#

I have an application with the overall directory structure with main directories as 'Check-in / Arrivals / Departures for the entire application. Models directory, controllers, and views all go according to this directory hierarchy. I need the application to startup at the departure section on the WIP page.
When I try to launch, I get this error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /
I have the router.config file setup with the following route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Departures/MasterWip", action = "Index", id = UrlParameter.Optional }
);
In the Controllers/Departures/MasterWipController.cs file, I have the following code:
public ActionResult Index()
{
IRepositoryFactory repoFactory = new RepositoryFactory();
IMasterWipRepository wipRepo = repoFactory.GetMasterWipRepo();
model = wipRepo.GetAllByNLIDate(new DateTime(2022, 1, 14));
ViewBag.Model = model;
return View("~/Views/Departures/MasterWip");
}
What am I missing or is incorrect?

in mapRoute just add the controller name
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MasterWip", action = "Index", id = UrlParameter.Optional }
);
and check with debugger if it hit the index view if it does just return view()
public ActionResult Index()
{
IRepositoryFactory repoFactory = new RepositoryFactory();
IMasterWipRepository wipRepo = repoFactory.GetMasterWipRepo();
model = wipRepo.GetAllByNLIDate(new DateTime(2022, 1, 14));
ViewBag.Model = model;
return View();
}

The ActionResult method must return the View("../Departures/MasterWip"); in which case is it looking for directory guidance during its parsing. The other option is to use the following:
return View("~/Views/Departures/MasterWip.cshtml");
where it is looking for a specific file.
This is in addition to Ahmed_Amr's comment about change to the router.config file.

Related

Why I get error when I try to hit action method

I use MVC 4 in my project.
I have an action method that I try to fire with this link:
https://localhost/Mobile/Map/Index/5/184614.560225826,666226.8163541115/7/3
The signature of the action method in Map controller is:
public ActionResult Index(int id = 0, PointF center = new PointF(), int zoom = 0, int rotation = 0)
Also, I defined this Permalink route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Permalink",
url: "Map/Index/id/zoom/rotation"
);
When I try to hit the method above, I get this error on the web page:
HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
While this link works perfectly:
https://localhost/Mobile/Map/Index/5
Any idea why the first link not works?

ASP.NET MVC RedirectToAction not redirecting to Index of another controller

In my Account/Login controller method, I have something like:
var classA = GetObject(); //actual code omitted
switch(classA.PropA)
{
case 1:
return RedirectToAction("Action2", "Registration");
//more case code omitted
default:
return RedirectToAction("Index", "Registration");
}
All the cases work fine in the switch block except the default where it's suppose to go to Index in RegistrationController. Instead of that, it takes me to localhost:port/Registration, where the action Index is omitted.
It works fine if the ActionName is changed to something else - Index2 for example. Also works fine if the controller name is changed to something else.
RouteConfig is
just the auto-generated code from creating the project, which is as follows:
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 }
);
}
Thanks in advance.
There is nothing wrong with the route setting the reason it does not include Index in the URL because according to default route
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
when you enter /Registration the route already knows the default action which is index so it does not add /index in URL and about
403.14 forbidden
if you look at this post HTTP Error 403.14 - Forbidden - MVC 4 with IIS Express it could be because you might have a file or folder named Registration in the project directory
If you use the RedirectionToAction("Index","ControllerName");
it will redirect you with the default mapping config to localhost:port/ControllerName
and in your case if you execute the RedirectionToAction("Index","ControllerName"); it will redirect you to
localhost:port/Registration
Try to use on
return RedirectToAction("Index");
and in RouteConfig you can route Action "Index" to Controller "Registration"
like
routes.MapRoute("Index", "Index", new { controller = "Registration", action = "Index" });

Trying to understand MVC routing

We have several controllers we would like grouped in a subfolder (Admin) on our site. We would have the main pages be at the root level. But for these pages we would like to have our site path be something like this:
www.domain.com/Admin/{controller}/{action}/{id}
I've set the RouteConfig.cs file like this:
routes.MapRoute
(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Submission", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute
(
name: "Admin",
url: "Admin/{controller}/{action}/{id}",
defaults: new { controller = "SystemSecurity", action = "Index", id = UrlParameter.Optional }
);
I've set up one of the controllers like this:
[RoutePrefix("Admin/SystemSecurity")]
public class SystemSecurityController : Controller
{
private MkpContext _db = new MkpContext();
// GET: SystemSecurity
public ActionResult Index()
{
var roles = _db.Role.Select(r => r);
return View(roles.ToList());
}
}
In our solution the path to the controller is: \Controllers\Admin\SystemSecurityController.cs
The path to the view is: \Views\Admin\SystemSecurity\Index.cshtml
But we get the 'Resource cannot be found' error message.
I've also tried it with no RoutePrefix, and also with RoutePrefix("Admin").
If I put the view here: \Views\SystemSecurity\Index.cshtml
and navigate with this path: www.domain.com/SystemSecurity/Index
the page loads, so I know the controller and page are working.
What am I doing wrong?
I found out about MVC Areas. (http://www.philliphaydon.com/2011/07/mvc-areas-routes-order-of-routes-matter/)
By adding an Area to my project (Right click on Project's name, then Add - Area) I am able to better group my code.
Many of the pages I found either don't mention Areas or when the do, it's in passing, but they don't explain them. Hopefully this will help somebody else.

MVC 4 routing "finding" controllers inside an Area

I have a MVC 4 WebSite with several Areas... I´m using all default routing created by VS2012...
So, I can access (from Area1) :
Area1/ControllerX/ActionX
I have some controllers without Area, so I can access :
ControllerY/ActionY
All fine... But, if I try to access the ControllerX without the Area1, like that :
ControllerX/ActionX
I got that error:
Exception: The view 'ActionX' or its master was not found or no view engine supports the
searched locations. The following locations were searched: ~/Views/mangavagao/ActionX.cshtml
~/Views/Shared/ActionX.cshtml
Controller: ControllerX
Action: ActionX
I was expecting a Not Found 404 error... Why is that route been captured ?
--
Area route:
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { controller = "ControllerX", action = "ActionY", id = UrlParameter.Optional }
);
Default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "ControllerY", action = "ActionY", id = UrlParameter.Optional );
Add the namespaces parameter in the default maproute function. Then set the UseNamespaceFallback datatoken to false.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MvcApplication.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
namespaces parameter is set to prioritize the controller finding, when multiple controller with the same name exists.
MVC will still search for controller outside of this namespace, if no match is found in the namespace.
UseNamespaceFallback datatoken tells MVC to disregard the (2) statement.
hope this helps.
Try to map the area route with a namespace:
context.MapRoute(
"Area1_default",
"Area1/{controller}/{action}/{id}",
new { controller = "ControllerX", action = "ActionY", id = UrlParameter.Optional },
new[] { "App.Areas.AreaName.Controllers" }
);
Change App and AreaName to the corresponding values.
This is similar to this question: Not including area name in URL results in "The view 'Index' or its master was not found" instead of 404
In your case the namespaces need to be added to the default route, not the area route. The namespace in this case should not reference the area controllers.
Something like this should work:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
null, // object constraints
new string[] { "Namespace.Application.Controllers" } // namespaces
);

How to route Mvc Controller action when there is subdirectory?

This type of url works: ABC/CSDS?id=314
Folder Structure:
Controllers - Folder
,ABCController - Class
,CSDS = Action
Global.cs file has:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "ABC", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
How to make change for subdirectory url like
ABC/TESTN/CSDS?id=314 to go to
Controllers - Folder
,ABCController - Class
,CSDS = Action
Currently it says "The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable"
How about adding a rule like this:
routes.MapRoute(
"Default", // Route name
"{controller}/TESTN/{action}/{id}", // URL with parameters
new { controller = "ABC", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Warning, not tested, I don't have the tools installed at work :(

Categories

Resources