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?
Related
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
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'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 am planning to write custom ASP.NET MVC4 routing for the problem described below.
By default [http://localhost:1603] URL should show login page after user logged in then URL should contain username name in the URL like [http://localhost:1603/username] and other action methods should access like [http://localhost:1603/username/profile].
Please guide me how to proceed with this problem.
Thanks in advance.
By default [http://localhost:1603] URL should show login page
To do this you can change Default Url Route's default values.Like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
You should change default Controller and Default Action name.For example I change them like: Account and Login.
URL should contain username name in the URL like [http://localhost:1603/username]
Now you need a custom route,maybe something like:
routes.MapRoute(
name: "Custom",
url: "{username}/{action}",
defaults: new { controller = "UserController", action = "Index" }
);
With this route (I assume you have (UserController and an Index action in it):
http://localhost:1603/username
Will go to the Index action of UserController.
http://localhost:1603/username/profile
Will look for a Profile Action inside of UserController.Ofcourse you should get username from RouteData dictionary inside of your Action.Otherwise that seems pointless.
I'm a fan of MvcCodeRouting (there's a NuGet package for it). Take a look at the doco for base routes. It will do what you're trying to achieve.