I am creating an e-commerce website in ASP.NET Core. I am facing an issue with routing.
The problem is with the Shop folder in views. I have nested folders in Shop, which are Men, Women, Kids & Accessories. These subfolders have views like Casual, Shirts....
I am unable to access these views (Casual, Shirts...) - trying to do so results in a HTTP 404 not found error each time.
Project directory is:
PROJECT DIRECTORY
There might be their things:
1-Check your Controller for return to view
2-If you didn't change the default routing, which is
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
You can access it with the that pattern
3-if you're sure about the two options above, check your
href atribute in your links
let me know if it's helped you….if it didn't, we can work on it
And this the MSDOC for routing
Related
When using a .NET MVC to build a website, when do I need to include a new route's info in RouteConfig.cs?
I have seen there is one default route pre-configured and registered in the RouteConfig.cs file like this:
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 }
);
}
}
When I created a new controller I saw that, without writing any route info in RouteConfig.cs, the corresponding view was rendered in my browser.
Even after creating a few more controllers, like testcontroller, that I accessed from test views, still without writing anything regarding a route for testcontroller, their views rendered properly.
As these views all render without editing anything in RouteConfig.cs, can you give me a few examples when I do need to write route info in RouteConfig.cs?
The RouteConfig exists for when you want to do a 'non default' route in MVC. As you can see, a lot of your ActionResults will be rendered by matching that route specified there. Ultimately you may be able to create an entire site and not even have to touch the RouteConfig.cs! It's only when you start to want different routes for use cases that you might find yourself diving in there.
An example for when you might need to edit it, is if you had an Area exclusively for blogs and wanted to do something along the lines of:
/Blog/Post/1234/my-blog-post
By default that wouldn't match the default config. In areas the default route config is prefixed by the Area name and then follows the usual standard like so.
/{area}/{controller}/{action}/{id}
In order to get override this we can write the following:
context.MapRoute(
"Blog",
"Blog/Post/{id}/{name}",
new { action = "Index",controller="Post" },
new { id = #"\d+" }
);
It's worth noting that in newer versions of MVC (5 onwards) we have access to the Routing attribute which is an easier and more accessible way to handle routing across controllers and actions.
Article: MVC5.1 Features, Attribute Routing..
Article: MVC5 Attribute Routing via David Hayden
Additional Reading for Routes in ASP.NET MVC
ASP.NET MVC Routing Overview
ASP.NET Typical Routing - URL Patterns
How to specify URL like DomainName/Admin/Folder/Controller/Action/Parameters in Asp.net MVC?Is It Require Routing?any one has any idea?please help me.
The routing framework is not filesystem based. It has no concept of what folder(s) the controller is in. You can get part of what you want via Areas, which allow you to group controllers under a common directory, but this is more than just a simple it directory. It has an actual class component that the routing framework uses in creating and interpreting the route. So for example, you could add an Area and then get a URL like:
/AreaName/ControllerName/ActionName/Parameters
But that's as far as you can take it.
Your only other option would be to use custom routes or attribute routing to "fake" the URLs you want. In other words, it still would have nothing to do with the actual filesystem path, but you could could define that the route should be prefixed with the static component of path you wanted.
routes.MapRoute(
name: "Default",
url: "Folder/SubFolder/{controller}/{action}/{id}",
defaults: new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
Or with attribute routing, you'd decorate your controller(s) with the RoutePrefix attribute:
[RoutePrefix("Folder/SubFolder/Foo")]
public class FooController : Controller
#Url.Action("Action", "Controller", new { area = "area", class="class", etc })
I Have faced same type of issue in my project. Specifying href for menus or for redirection fails when they keep that is folder subfolder.
Better option would be href with Url.action in html links.
<a class="nav-link" href="#Url.Action("ActionmethodName", "Controller Name", new { name = "John", contact = "calgary, vancouver" })">Home</a>
Use Full path Url for Ajax Calls:
specify full url for url param for ajax calls.
I am trying to create a new route... Here the code:
routes.MapRoute(
name: "Services",
url: "Administration/{controller}/{action}/{id}",
defaults: new { controller = "Services", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Now when I click on an actionlik, it redirects me on the correct controller:
#Html.ActionLink("Services", "Index","Services")
Here the Index Action in the Services controller
public ActionResult Index()
{
return View(); //Here it is where I stop debug
}
I arrive in the action.. and now my custom route should redirect to my view. Correct?
I let you see what I see when i stop debug:
How you can see everything seems is well valorized. But when I obtain following error:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Services/Index.aspx
~/Views/Services/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Services/Index.cshtml
~/Views/Services/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
It does not look the view inside Administration folder!!
In this way it works instead:
return View("~/Views/Administration/Services/Index.cshtml");
Where is wrong?
Thank you
Looks like you don't quite understand routing, or that routing is not the same thing as view resolution. Changing the URL to access an action method doesn't make it search new folders for your views. So you need to do one of the following:
Leave the view in one of the folders the error message suggests.
Manually specify the view: return View("~/Views/Administration/Services/Index.cshtml");
Learn how to use MVC areas.
Manually add in your additional folders to the list the view engine uses.
Implement your own view engine.
Personally I recommend option 1.
I'm new to the whole routing thing in MVC so I'm not sure what to make the title of my question. Basically, we have Google Analytics that pics up stats based on the current url. We added in a referrer to the route so that we can give a link to our partner sites such as:
http://www.mysite.co.za/PartnerSite/home/index
When the user comes to our site directly, ie: http://www.mysite.co.za, I want the url to show "Website" as the referrer.
So in essence,
http://www.mysite.co.za
must change to
http://www.mysite.co.za/Website/Home/Index
My route is current configured as:
routes.MapRoute(
name: "Search",
url: "{referer}/{controller}/{action}",
defaults: new { referer = "Website", controller = "Home", action = "Index" }
);
Can this be done in the routes or should I get my system administrator to update IIS settings and redirect?
Thanks,
So to close this off I needed to have a solution posted.
The bottom line is that this is something for IIS, and it does not seem possible using Routes.
ok,suppose when the user hits the url http://www.mysite.co.za then suppose it accessing Home Controller's Index View Action.Because /Home/Index is the First Default Page in MVC so its not the Part of URL.
So for your Case You should write the code like this-
routes.MapRoute(
name: "Website",
url: "/Website/Home/Index",
defaults: new { controller = "Home", action = "Index" }
);
Please Try it and let me know the result.
I have created an ASP.NET WebApplication with Internet Template.
I have NOT modified anything in the RouteConfig class, meaning I am using default Route table only.
When I type the following URL insde the browser
http://localhost:8416/
Index view is retuning insde HomeController without any problem.
But, if I type the following URL
http://localhost:8416/EstimationTracker/
I am gettinger the following error.
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
But, if I type the following URL, view is returning fine (just appending /Index to the above URL).
http://localhost:8416/EstimationTracker/Index
My doubt is why Index action method is being not recognized as the default action method inside EstimationTracker controller or is something else happening?
For easy referece, the following is the code inside inside RouteConfig.cs file.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
EstimationTracker folder created in the project root folder is causing the problem.
The error in the question means that the static file handler took the request.
I got the answer from here.
Is there a folder called EstimationTracker in you application's root folder?