Home Index not loading when the application starts - c#

I am working on an ASP.NET MVC3 application.
In the Views I created a folder named Home and a View named Index.
Then I created a Controller HomeController.
In the Controller I added:
public ActionResult Index()
{
return View();
}
but when I run the application I get this error:
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: /
If I add to the address bar : /Home/Index the view loads normally.
How can I make the application automatically go to Home/Index when it loads?
Thanks for any help

You need to add a route to the Global.asax file that points to your path.
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Related

MVC Resource Can not be found After Publish

I have an MVC Web Application.
When I am running the application from my code, that works fine and I am able to go any pages.
However I publish it on IIS, It gives the error:
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.
And the URL Changes like below:
http://localhost/IssuerScripting_Web/%23/%23
However the path is
http://localhost/IssuerScripting_Web/UserRoleManagement/User_Management
EDIT: Here is my RouteConfig in below.
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboards", action = "Dashboard_1", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Password",
url: "AdminUserPasswordCreate/PasswordCreate/{id}/",
defaults: new { controller = "AdminUserPasswordCreate", action = "CreatedPassword", id = UrlParameter.Optional }
);
}
And the below is the controller of Dashboard:
public class DashboardsController : BaseController //BaseController
{
public ActionResult Dashboard_1()
{
return View();
}
}
What might be causing this issue?
My default login page is working fine. But this page is like a main page and has all the Menu Items.
I was able to publish it earlier, and was working when I publish. I am not able to figure out the issue.
I will be thankful for any help.
To whom has this problem, After several hours I figured out that Scripts files were not pasting properly when I published it.
I removed the Scripts, Styles, Content folders from my project and added them back.
Then I published and it worked!

Program trying to load the cs.html file, giving a 404

I am getting a HTTP 404.
I don't know what I did that suddenly started causing this error. I changed the startpage, then it began reporting this error.
I have found the file RouteConfig.cs (guessing this is where the solution might be) which looks like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Member", action = "AddMember", id = UrlParameter.Optional }
);
The error I am getting says that the ressource could not be found, reports this URL:
http://localhost:53043/Views/Member/AddMember.cshtml
I noticed that it suddenly want's to look for the .cshtml file. But isn't this wrong?
The controller to the AddMember View looks like this:
public ActionResult AddMember()
{
return View();
}
If I rightclick on the AddMember.cshtml file, and choose View in browser, then the page show up just fine, and the URL looks like this http://localhost:53043/Member/AddMember
Any idea to how I can solve this pls?
If I rightclick on the AddMember.cshtml file, and choose View in browser, then the page show up just fine, and the URL looks like this http://localhost:53043/Member/AddMember
The expected url for any cshtml page (or ActionResult of a controller) in MVC C# is http://localhost:{port}/{controller}/{action}/{id} where id is optional as stated in the route config.
The way you're trying to view the page http://localhost:{port}/Views/{controller}/{action}.cshtml is the file directory of the view and not the way to view a cshtml file in the browser.
To view a result of any cshtml file, simply press F5, Ctrl + F5, or right click and view in browser, they all return same result. Good luck.
The URL is wrong , you cannot directly load the ".cshtml" file. In MVC , you need to call the controller method which then calls the view. If your controller name is "Member" and method name is "AddMember" then the URL should be listed as mentioned below. Hope This Helps!
http://localhost:53043/Views/Member/AddMember.cshtml

How to MapRoute to a sub controller

I have a file, AdminController.cs, that currently holds the controllers for every action in the admin section of my site. Obviously, this is getting huge, and I'd like to delegate control to a different controller for each application.
For instance:
www.mysite.com/Admin/Car currently looks to AdminController.cs to decide what to do. So when a user adds a car, there is an ActionResult in AdminController.cs called AddCar(). I would like this instead to look to CarController.cs to find AddCar().
So I after some research, I added this to my RouteConfig file:
routes.MapRoute(
name: "/Admin/Car"
url: "{parent}/{controller}/{action}",
defaults: new { parent = "Admin", action = "Index" },
constraints: new { controller = "Car"}
);
I commented out the ActionResults related to 'Car' in AdminController, and added them to CarController.
However, I'm getting "The resource cannot be found." when I navigate to www.mysite.com/Admin/Car.
How can I use a different controller, but with the URL still in the Admin realm?
You could add a controller action 'Car' to the Admin controller which redirects to the relevant action in the Car controller. But, Areas are probably what you want.

ASP.NET MVC default routh is wrong

I got simple web application with 1 default routing:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Game", action = "Index", id = UrlParameter.Optional }
);
My controller contains the following actions:
public class GameController : Controller
{
public ActionResult Index()
{
// some actions
return View();
}
[HttpPost]
public ActionResult CreateGame(Game game, User user)
{
// some actions
return View("Game");
}
[HttpPost]
public ActionResult JoinGame(User user)
{
// some actions
return View("Game");
}
}
Also under Views/Game folder I got "Index" and "Game" views.
But when I start application from time to time (NOT ALWAYS!) it requests
http://localhost:55815/Game/Game
instead of
http://localhost:55815 or http://localhost:55815/Game/Index
Your application default route is working fine.
The debugger starts url http://localhost:55815/Game/Game because the file Game.cshtml is currently opened in your Visual Studio.
The solution is on your VisualStudio project configuration.
Choose specific page with empty value instead of current/active Page.
Actually,
/Game/Game
and
/Game/Index
are both the same. When you check you default routing file you can see that your main controller is written with predefined controller and action. So the program automatically resolve this url according to that. Mostly try your web application not in visual studio debug but put it under IIS than if you need debugging, debug IIS instance.
Good luck

adding custom view name in asp.net mvc4

I am building an application in .net mvc4 that is based on business promotion and sales store.
User on this web application would be able to use product and access his/her personal business page also, that page can be promoted in future.
So I added one controller- Mypanel and a view of user's personal or professional business page _Mypanel.
Now the url access to this page is Bizcopter.com/Mypanel/_Mypanel
I want a custom user defined page name-
i.e. If a business name is - BookStore
Then I want to add a view in this same controller with the name of BookStore, So URL of personal business page would be-
Bizcopter.com/Mypanel/BookStore/ and this business holder can promote his business page with this URL.
Let me know if these are possible-
Replacing the view's name of user's choice
Add a view from client side in this same controller
I don't have any idea how to make it happen so don't have any trying code.
Site URL- http://bizcopter.com/Mypanel/_Mypanel
You don't need a separate view and controller action for each business.
I would create a controller and view called MyPanel. The controller takes a parameter called something like businessName that will load data related to the parameter.
By default you'll have a route in your AppStart/RouteConfig.cs which may look like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
The default URL structure might look something similar to: http://localhost:{PortNumber}/{Controller}/{Action}
Where we can attribute the following:
Controller = Home
Action = Index
Now if you want something similar to how you have it, you'd want something along the lines of:
http://testing.com/Fruits/Apples
Controller = Fruits
Action = Apples
By default, a URL pattern will match any URL that has the correct number of segments, in this case {controller}/{action}
Overall you should just need the MyPanel controller, and a controller taking a parameter of string which loads the correct Object/Model into the view.
Source: Pro ASP.NET MVC 4 - Adam Freeman
As the others said you can add a new route.Consider this code:
routes.MapRoute(
name: "MyCustomRoute",
url: "MyPanel/{name}"
defaults: new { controller = "MyPanel", action = "MyAction", name="" }
);
In this route if user type this URL:
Bizcopter.com/Mypanel/
Then it goes to your MyAction in your MyPanel Controller by default.Actually it will always go to MyAction, and in your MyAction, you must take the name parameter and redirect to user to the Relevant Action like this:
public ActionResult MyAction()
{
var name = RouteDate.Values["name"];
// check the name and redirect user to another action if it necessary
if(name == "BookStore") return RedirectToAction("BookStore","Mypanel");
}

Categories

Resources