Navigate to Index view of another controller in ASP.NET MVC 4 - asp.net-mvc-4

I am Inside Index View of Home controller.
I have created one controller called EstimationTracker with default Index action method. I have also created Index view of EstimationTracker. But, nothing is there inside Index action method of EstimationTracker controller.
Now, I want to create Html.ActionLink inside Index view of HomeController to navigate to Index view of EstimationTracker controller.
I have written the following code to achieve this:
#Html.ActionLink("Estimation Tracker", "Index", "EstimationTracker", null, new { })
In the Browser, if I click on "Estimation Tracker" hyperlink, the page is generating the following URL:
http://localhost:8416/EstimationTracker/
and popping up the following error:
HTTP Error 403.14 - Forbidden
The Web server is configured to not list the contents of this directory.
When I append Index to the above URL i.e.:
http://localhost:8416/EstimationTracker/Index
Now, the page is fine.
How can I make the page run smoothly without any error.
Update:
I'm using Default Routing only. like below
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

How to remove controller name from url in asp.net mvc?

i am working on a web project in asp.net mvc.
This is code in my RouteConfig
This is url
localhost:55960/Home/myAction/80102/Aus-won-the-match-by-9-wickets
but i want to make it like
localhost:55960/myAction/80102/Aus-won-the-match-by-9-wickets
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{Title}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, Title = "" }
);
I have applied many suggestions available on this platform but not worked.
Is there any one who can explain in detail
So you want your controller to be at the root, like domain.com/?id=1 or domain.com/list?id=1
instead of domain.com/controller/list?id=1 ?
You can add to your config
config.MapHttpAttributeRoutes();
Then for that controller,
[RoutePrefix("")]
public class HomeController : Controller
But this may get confusing to keep track of if you have many controllers.
And it may be better to configure routes.MapRoute() to point to a default controller

Mvc Routing does not work even if the route is matched

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.

ASP.NET MVC4 custom routing

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.

ASP.NET MVC 4 site returns 404 for image

I think this might be a routing issue, but I am not sure how to fix.
I have an image at...
<img src="/blog/images/scheduleAutoCalculation1.png">
and have verified it exists in that path. It should pull up. However, it returns a 404.
If I copy the image and load it with
<img src="/Resources/scheduleAutoCalculation1.png">
it pulls up fine. Here is my route table...
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 }
);
}
}
Any ideas on why I am getting a 404 for the first link?
Also, I don't have a BlogController class, so it is not trying to route to that (maybe it is??). The site is simple with only two controllers HomeController and StyleController.
I was trying to load images from another Views type of folder I had created. The web.config located in that directory was blocking the request. I moved the images to another folder and it works fine.

Navigation is not working with Default Route table in MVC 4

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?