Problem while adding new Views in ASP MVC 5 - c#

I am using ASP.Net MVC 5 for web development. I have added many Views and they are working. But if I try to add a new View, it cannot be navigated by browser i.e. 404 error occurred. But the rest of the Views are working correctly.
I tried to add new Views in distinct controllers but they have the same problem.
Please help me to solve this issue.

Created a new view in MVC 5, opening the new view results in HTTP 404
You should be accessing the view through an action method. So If you
created your new view in ~/Views/Home/AboutMe.cshtml, You should add
an action method like this in your HomeController.
public class HomeController : Controller
{
public ActionResult AboutMe()
{
return View();
}
}
Now you can access this like
http://yourServerName/yourAppName/Home/AboutMe
If you want to have your action method in a different controller, You
can specify the full view path. Ex : IF you want to add the action
method to your Account controller,
public class AccountController : Controller
{
public ActionResult AboutMe()
{
return View("~/Views/Home/aboutme.cshtml");
}
}

Related

Controller hangs when opened using url in mvc 5

I'm new on asp.net mvc. I created a basic controller and I tried to open it using url. But rendering is not finished and my controller didn't display in several minutes. I did not change anything from default asp.net 5 mvc project also my controller's index method return only hello world string. I don't is there any problem on iis or VS. Any idea about that problem?
Thanks for help.
In MVC only public methods that return an ActionResult are accessible as web pages.
So you MUST use something like this:
public class HelloWorldController : Controller
{
// GET: HelloWorld/Index
public ActionResult Index()
{
return Content("This is <b>Index</b> action...");
}
// etc
}
Content(...) is a special method the wraps text into an ActionResult.
Note: only use Content(...) if you specifically do NOT want to use a View such as Index.cshtml - which is what you normally WOULD do, of course.

Routing with Dashes in ASP.NET MVC

I am working on an app with ASP.NET MVC 5. I want my app to have a route that looks like the following:
http://www.myserver.com/my-category
Please notice how the route has a dash (-) in it. I currently have a controller named MyCategoryController. It is defined like this:
namespace MyApp.Controllers
{
[RoutePrefix("my-category")]
public class MyCategoryController : Controller
{
// GET: List
public ActionResult Index()
{
return View();
}
}
}
The view is located in /Views/My-Category/Index.cshtml. When I try to access http://www.myserver.com/my-category in the browser, I get an error that says:
The resource cannot be found.
I set a breakpoint and I noticed that the breakpoint is not hit. I then enter http://www.myserver.com/mycategory into the browser, and I get an error that says:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/mycategory/Index.cshtml
~/Views/mycategory/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
How do I setup my ASP.NET MVC so that
a) I can visit http://www.myserver.com/my-category and
b) Load the view from /Views/my-category/Index.cshtml
You need to name the views folder like the controller not like the route.
So /Views/MyCategory/Index.cshtml and not /Views/My-Category/Index.cshtml.
If you, for a reason I can't imagine why, want it to be /Views/My-Category/Index.cshtml you need to "fully quallify the view":
return View("~/Views/My-Category/Index.cshtml");
About the route with the dash: I am not using attribute based routing so I can only guess:
Did you add the routes.MapMvcAttributeRoutes(); in your RegisterRoutes method?
Because http://www.myserver.com/mycategory is routed by the default "{controller}/{action}/{id}" route...

POST data using RenderMvcController in Umbraco

I have set up a document type in Umbraco, and have created a custom controller & model for this document type.
The custom controller inherits from : RenderMvcController class and the views inherit the model through #inherits Umbraco.Web.Mvc.UmbracoViewPage<com.something.model>
This all works fine for any HttpGet requests. However as soon as I want to do a form post back to the controller using #using (Html.BeginUmbracoForm("SomeAction", "SomeController", null, new { #class = "some-class" }))
I get the following error message: Could not find a Surface controller route in the RouteTable for controller name SomeController
From all the documentation that I was able to find it always refers to SurfaceControllers when it comes to form posts. Is there a way to change the routing so that it would post to my custom controller, rather then another controller that inherits from the SurfaceController class?
If you are going to post the form in this way, you need two controllers. One for the Document Type that inherits from MvcRenderController (as you already have) and a second which inherits from the SurfaceController.
The surface controller just needs a single POST action that does one of the following things:
// e.g. if modelstate is invalid
return CurrentUmbracoPage();
// redirecting back to original page after a successful post
return RedirectToCurrentUmbracoPage();
// Redirecting to another page after a successful post
return RedirectToUmbracoPage(12345)
This has been taken from the documentation here: http://our.umbraco.org/documentation/Reference/Templating/Mvc/forms
Strictly speaking the initial document type controller is not necessary in this scenario as it does not play a part in the post of the form.
If you want to post directly to the custom controller then you should not use Html.BeginUmbracoForm, you should just post directly to the current URL. The complication here is that it is then a little tricky to bind your model as a parameter of the post action. Your view model will have to inherit from RenderModel in the following way:
public class BaseModel : RenderModel
{
public BaseModel() :
base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
}
According to http://our.umbraco.org/forum/developers/api-questions/38662-(v6)-Could-not-find-a-Surface-controller-route-error?p=0,
SomeController Needs to inherit from SurfaceController, not RenderMvcController.
Alternatively, you could create a dedicate route and point it directly to your controller:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("someController", "some/someAction",
new { controller = "Some", action = "SomeAction" });
}
}

how does asp.net mvc relate a view to a controller action?

I have opened a sample ASP.NET MVC project.
In HomeController I have created a method (action) named MethodA
public ActionResult MethodA()
{
return View();
}
I have right clicked on MethodA and created a new view called MethodA1
Re-did it and created a new view called MethodA2.
How is this magical relationship done? I looked for the config to tell the compiler that views MethodAX are related to action MethodA, but found none.
What view will the controller return when MethodA is called?
The convention is that if you don't specify a view name, the corresponding view will be the name of the action. So:
public ActionResult MethodA()
{
return View();
}
will render ~/Views/ControllerName/MethodA.cshtml.
But you could also specify a view name:
public ActionResult MethodA()
{
return View("FooBar");
}
and now the ~/Views/ControllerName/FooBar.cshtml view will be rendered.
Or you could even specify a fully qualified view name which is not inside the views folder of the current controller:
public ActionResult MethodA()
{
return View("~/Views/Foo/Baz.cshtml");
}
Now obviously all this assumes Razor as view engine. If you are using WebForms, replace .cshtml with .aspx or .ascx (if you are working with partials).
For example if there is no view it will even tell you where and in what order is looking for views:
Remember: ASP.NET MVC is all about convention over configuration.
The MVC framework use convention over configuration. The framework calls the ExecuteResult on the ViewResult object (created by the return View();). The framework by convention then looks in a number of locations to find a view
If you are using areas the framework will look in the following locations for a view.
/Areas//Views/ControllerName/ViewName.aspx
/Areas//Views/ControllerName/ViewName.ascx
/Areas//Views/Shared/ViewName.aspx
/Areas//Views/Shared/ViewName.ascx
/Areas//Views/ControllerName/ViewName.cshtml
/Areas//Views/ControllerName/ViewName.vbhtml
/Areas//Views/Shared/ViewName.cshtml
/Areas//Views/Shared/ViewName.vbhtml
Without areas (or if you are using areas and no view has been found) the framework will look at the following locations
/Views/ControllerName/ViewName.aspx
/Views/ControllerName/ViewName.ascx
/Views/Shared/ViewName.aspx
/Views/Shared/ViewName.ascx
/Views/ControllerName/ViewName.cshtml
/Views/ControllerName/ViewName.vbhtml
/Views/Shared/ViewName.cshtml
/Views/Shared/ViewName.vbhtml
As soon as the Framework tests a location and finds a file, then the search stops,
and the view that has been found is used to render the response to the client.
There are a number of overriden versions of the View method. The most common one is to render a specific view, outside of the framework convention, by calling it by name. For example
return View("~/Views/AnotherIndex.cshtml");
As an interesting footnote, the framework looks for legacy ASP, C# and VB Razor views (aspx, ascx, cshtml and vbhtml) even though you have a specific view engine.
In MVC controller action is not bound to view.
It uses delegate mechanism to pickup the view.
Model Binding(Mapping)
I was looking for the same and I just made a couple of tests and figured out.
It doesn't save anywhere.
To understand how it works; just do these steps:
In your controller, right click, Add View
Then enter a different View Name
and Ctrl F5
you will get Server error in application.
For example if you right click , Add View in following Index action method and type "Index2" in View name, you will get the error.
public class TestController : Controller
{
// GET: Test
public ActionResult Index()
{
return View();
}
}
So basically there is a 1-1 matching between action name and View name. And you cannot add view for the same method so there is no need to save in a config file.
Now change the view file name in Visual Studio from Index2.cshtml to Index.cshtml then Ctrl+F5. You should see it is working.

MVC-3 ASP.NET Shared Views-Redirect-Razor

I have a shared view called NotAuthorised in the folder 'Views/Shared'. I want to redirect the users to this view when they are not authorised to see the page.
Initially, this view was in a folder called Account. But I moved it into the Shared folder as I am not using the Account anymore. I have deleted the Account folder.
I used the following code to redirect:
public ActionResult NotAuthorised()
{
return RedirectToAction("NotAuthorised", "Account");
}
Now that I removed the Account folder, I'm trying to use
public ActionResult NotAuthorised()
{
return RedirectToAction("NotAuthorised", "Shared");
}
I am completely wrong by giving the folder name shared in the last line.
Could anyone tell me, what I am doing wrong?
Thank you
You can't redirect to a View, only to an Action of a Controller. You have to specify an controller action for your redirect and there you can render your shared view.
public class AuthorizeController : Controller
{
public ActionResult NotAuthorised()
{
return View("NotAuthorised");
}
}
and later redirect to this new action from within any other action method:
return RedirectToAction("NotAuthorised", "Authorize");
But you may not need this additional Controller. You could simply render the shared View
public ActionResult NotAuthorised()
{
return View("NotAuthorised");
}

Categories