MVC asp.net Resource cannot be found - asp.net-mvc

I am new to asp.net MVC. I have added one controller and view and set route to view my index page. But it is giving me following 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: /

From your comments I take it, that you misunderstood how the basics of MVC work. (You can read it all here: https://www.asp.net/mvc)
By default MVC has a lot of conventions, so it knows how a route maps to a controller amongst other things. When you issue a request to an url, the default implementation of the MvcHandler receives a controller instance from the default ControllerFactory implementation, which has a convention, that a controller must end on the name Controller. That means, that /Home1Controller/ in your example, lets the controller factory look for a controller class named Home1ControllerController, which most likely does not exist.
For completeness, here are the criteria by which thew default controller factory identifies the correct controller:
class scope must be public
class must not be abstract
class must not take generic parameters
class name must end on "Controller"
class must implement IController interface.
So if you keep the default route, which you have and you have created a controller called Home1Controller, then the route on which you can access the Index method is: /Home1/Index.
If your controller would be namend FooController, then the url would be /Foo/

Related

What calls Controller class's methods in MVC

A general question in MVC. We know, in order to call a method, an object need to be created for the class containing the method and call then call it with that object.
My question is, in MVC we only define the default controller name and 'ActionResult' in route config.
Now what is creating object for that controller class and calling method(ActionResult) with that object?
It's the MVC framework itself. Based on the route, method etc it's supposed to handle, it will instantiate a particular controller class and invoke the specified method/s for it to handle that route.

How does the URL match a Controller class in MVC?

Suppose I create a Controller named Login, the IDE generates a file like
Public Class LoginController
Inherits System.Web.Mvc.Controller
'
' GET: /Login
Function Index() As ActionResult
Return View()
End Function
End Class
So when I run my app and enter the following URL
http://localhost:49599/Login
The controller fires, the view is served, etc. But wow does the word "Login" get wired to this LoginController class? What happens if I want to have the option of using a different language and I want to change "Login" to "Logzmein". I'm suspecting resource files should be involved somewhere.
This is part of the convention-based design of ASP.NET MVC. By default, there are default routes defined that route (or convert) a URL into a controller action method.
By default, routes are defined using the following convention:
{controller}/{action}/{id}
So given a URL like this:
User/View/1
It break down like this:
User ({controller}) maps to UserController controller class.
User/View ({controller}/{action}) routes to UserController.View action method.
User/View/1 ({controller}/{action}/{id}) routes to UserController.View action method, passing in 1 as the id parameter.
You can also define custom routes to create your own patterns for routing.
Check out this article for an overview or routing, because it's too much to cover in a single post.

Routing/Resolving asp.net mvc

I have a question that I have been thinking about.
What happens from when I enter an URL in my browser and click enter to when the code in the corresponding controller actually executes?
Let's say I have an asp.net mvc application and a trivial Controller like this:
public class HomeController : Controller
{
public ActionResult About(int id = 25)
{
//code
}
}
So if I enter like myUrl/home/about?id=56 that controller gets invoked. I know there is a routing pattern like {controller}/{action}/{id} declared by default. But if an URL matches that pattern is a controller instance created then or what happens? Is there some Resolver class or similar? Is there like a List containing all the controllers so "Home" in the URL matches the HomeController?
I know the id parameter in the controller signature will be bound to the corresponding id value in the request context with the help of model binding. But there must be a lot of other magic happening here.
Hope someone can help a fresh confused asp.net mvc user
in a nutshell:
DefaultControllerFactory finds and instantiates the controller by its name using reflection (more info)
ControllerActionInvoker finds and invokes the action by its name using reflection (more info)
Good overall description can be found here.

Grails Custom Scaffolding get access to controller name

I am trying to write a custom src/templates/scaffolding/Controller.groovy and was wondering if there was any way to get access to the controller name? Right now it seems like you can only get the "model" class. The reason I need it is I am customizing the render to prefix the templates directory based on the controller.
For instance I have a controller named AuthorAdminController and I need to customize the list to use the /admin/user/** directory.
Let me know if you have any questions. I am getting ready to look into how to customize DefaultGrailsTemplateGenerator but I am not sure if that is the correct route to go.
Example:
class UserAdminController {
static scaffold = User
}
Currently in my Controller.groovy I get className='user' so I have no access to the controller.
I don't think you can, as the way scaffolding works your template will always be generating a class named DomainClassNameController (i.e. UserController in your example), which gets loaded into a new classloader and then the metaclass of the real controller (UserAdminController) gets new actions added to it which delegate to an instance of the generated UserController.
Now every controller has access to the controllerName property during execution of actions, so this may provide you with a workaround. I haven't tried it, but you could try putting a log.info("controller: \${controllerName}") into the template and see which name it gives you (the backslash to make it resolve at runtime rather than generation time).

ASP.NET MVC basic routing with no View name

This is a basic question about the routing machinery. From a new MVC project, I have this in the HomeController:
public ActionResult MyPage()
{
return View();
}
In the Views\Home folder, I have MyPage.aspx. The routing is still the default of {controller}/{action}/{id}. When I type in http://localhost:1790/Home/MyPage, it is correclty routes to MyPage.aspx. Since I haven't said anything about which view to render, how does ASP.NET MVC know to correctly route to MyPage.aspx? Looks as though the ActionResult name can also be used as the View/aspx page name...unless there is something I misunderstand in how the routing works. I can see how I end up in the Home folder, since the controller name corresponds to the View sub folder name. But does the Action name also correspond to the aspx name?
Would that work if the page was a PHP?
ASP.NET MVC subscribes to what is known as the Convention over Configuration paradigm whereas if you follow their conventions, basic things such as routing concerns will happen for you. But they also allow you to configure them if desired.
MVC implicitly assumes that if you return just View(), that you want View("MyPage") (i.e. the action name). No sense in repeating yourself unnecessarily.
It won't find a PHP file by default, but I'm sure you could override that behavior if you really wanted to. I can't imagine a sane scenario where you would be mixing PHP and ASP.NET MVC, but who knows :)
Action name is the same as the view / partial view name.
asp.net mvc doesn't work with php as far as I'm aware.
As has already been stated, ASP.NET MVC uses convention over configuration. Out of the box, your folder structure is something like this (only showing relevant portions and doing it from memory so...)
Site Root
+ Controllers
HomeController.cs
AccountController.cs
+ Views
+ Home
Index.aspx
+ Account
Index.aspx
+ Shared
The default routing handler is something similar to the following:
"{controller}/{action}/{id}"
There are default values for the route, but if you have a url that is a/b/c, it will look for action a on controller aController and pass it c as a parameter if said method on the controller accepts parameters.
A couple of things about that then need to be clarified. Again, convention over configuration:
1) All controller classes must end with Controller if you're using the default engine. That way, when a request comes in and the {controller} value is parsed, the engine adds Controller to it, looks in the Controller folder (and, thus, namespace), and locates the class.
2) By default -- this can be changed -- all views for a controller must reside in the Views/{controller} folder or in the Views/Shared folder.
3) Public methods on a controller are, by default, actions. You can hide this with an attribute to make themethod unavailable to the engine, but by default they are public.
So, when a request comes in the route is compared against all known routes (global.asax) and the first route that matches the request is accepted. The route is then parsed into the component parts to determine the controller, action, and parameters for the action.
Once the controller is identified, the engine instantiates an instance of that controller and executes the matching method (action) should it be found.
The action will return an ActionResult. View is an extensino method that actually returns ViewResult (if I remember that correctly). The default view for an action is a view of the same name as teh action residing in the Views/{ControllerName} folder.
Routing is a beast unto itself and I'd recommend a good bit of reading on it if you're going to play with it. Minutes to understand but a lifetime to master sorta thing.
To my knowledge, BTW, there is no engine that will use a php page as a view for a controller action.

Resources