Trying to display Custom View in MVC app but it says requested URL not found - asp.net

I am new to azure, MVC and also ASP.NET. I am writing MVC Cloud service with ASP.NET web role. Please help me with this problem
When I create the application there are default views but I wanted to see my view so I set my view as start page. I also changed the values in RegisterRoutes method
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "User", action = "AddUser", id = UrlParameter.Optional }
);
}
When I run the app, it gives HTTP 404 error because it could not find request URL : /Views/User/AddUser.cshtml

In MVC you don't put the view in the URL to get it rendered.
This won't work: /Views/User/AddUser.cshtml
As you've correctly put in your question the default route is {controller}/{action}/{id} with id being optional.
So assuming that User is your controller, i.e. you have a class called UserController, which looks something like:
namespace My.Controllers
{
public class UserController : Controller
{
which has an action on it called AddUser:
public ActionResult AddUser()
{
// implementation logic
return View();
}
Then the default route will display your view when it processes the URL /User/AddUser
In MVC 5, this looks something like:

Related

ASP.NET MVC route only works on server but not on local development machine

When I try to access the Index view of my controller, I get an HTTP 404 error.
But if I deploy the app on the server, everything works fine. Even if I access my action by typing in the URL User/Index.
The problem is it can't find the Index action on its on.
I don't have this problem with other controllers. For example I can input: /Weather, /Forms just fine, and it goes directly into the Index action methods of those controllers.
This is my UserController and its Index action method:
public class UserController : SerializeController
{
private readonly IUsers Users;
public UserController(IUsers users)
{
Users = users;
}
public ActionResult Index()
{
return View();
}
......
}
and my RouteConfig.cs :
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional });
It's weird - why is this only happening on the Index action of that one controller - and not others?
I'm also using Autofac dependency injection and registering my interface like this :
Container.RegisterType<Users>().As<IUsers>().InstancePerLifetimeScope();
For Calling this controller , Ive just using simple browser call e.g.:
http://localhost:2569/User , and it shows :
And Also Chrome network tab result :
But if i publish it into IIS and call example.org/User , the action opens without any problem.

Some Controllers not going to default Index action

I have a MVC project. I added a few controllers; over time edited in some code some controllers. Might have done something where I landed up in the following problem and now I do not know how to fix it.
Now http://server/Controller1 correctly executes Index action. But http://server/Controller2 does NOT execute the Index action; instead I get "The Web server is configured to not list the contents of this directory." http://server/Controller2/Index works as expected.
I have gone through similar questions. As you can see Controller1/ routing is happening properly. So it is not IIS config. Controller2 has index() function; it is not route config issue as well; I have not added any specific route for Controller1 or Controller2. The route is basically the default route
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 }
);
}
It sounds like you may have a folder in your website called Controller2, as the webserver is trying to list its contents

Running an action method on every view

I want to show the version info of the current project on every page.
The way I do it in the moment is like this:
Inside the Index method of my homecontroller:
ViewBag.Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
Inside the _layout.cshtml(masterpage):
#ViewBag.Version
The Problem here is, it will only displayed once, but I want to display the Version on every page/view.
This is my routing configuration:
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 }
);
}
Do I have to change something here ?
Ty for helping
Why not just call
#System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
on the _Layout.cshtml view?
MVC has a different launch context and a work around is needed to text the version:
#typeof(HomeController).Assembly.GetName().Version
HomeController could be replaced with any other type in the assembly the MVC application assembly. See this question for more info.
Just put this in the Layout.
<label>#System.Reflection.Assembly.GetExecutingAssembly().GetName().Version</label>

default page issue with 4.0 ASP.NET and MVC mixed mode website

I am converting a classic ASP.NET 4.0 site to also use MVC. Over time I am migrating the ASP.NET code to MVC, but during the transition both technologies will be in use.
If I navigate to the default page (ie, http://mywebsite.com/), then MVC routing is taking over and returning the following message.
This request has been blocked because sensitive information could be
disclosed to third party web sites when this is used in a GET request.
To allow GET requests, set JsonRequestBehavior to AllowGet
If I use http://mywebsite.com/default.aspx, then everything works fine.
My route config looks like...
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//ignore aspx pages (web forms take care of these)
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Notice I am ignoring .aspx page requests, so these requests get ignored by the MVC pipeline. However, I need 'no page specified' default requests to process default.aspx. How would I change the above code or configure the site/IIS to make this happen?
I removed the default route and now the behavior is as needed.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//ignore aspx pages (web forms take care of these)
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id = UrlParameter.Optional }
);
}

ASP.NET MVC route returning 404 without action

I am working on a very simple application, using MVC2 Preview 1.
I have a controller named ContentController. My problem is that /Content/Index works correctly, but /Content/ returns a 404. I am running the application on the Studio Development Server.
Tested with RouteDebugger but /Content/ returns a 404, and does not display any debugging information.
I have not changed the routing code:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
This is my controller:
public class ContentController : Controller
{
IRepository _repo = new SimpleRepository("db", SimpleRepositoryOptions.RunMigrations);
public ActionResult Index()
{
var content = _repo.GetPaged<Content>(0, 20);
return View(content);
}
It's a shot in the dark, but do you have a directory named /Content/ as well?
/Content is a controller, which is basically just a collection of actions. ASP.NET MVC needs to know WHICH action you want to run, so by leaving out the action asp.net mvc doesn't know what action to return and gives a 404.
You can tell it a default either by adding a route:
eg:
routes.MapRoute("ContentDefault", "Content", new {controller = "Content", action = "Index"});
The attributes are defined as follows:
'ContentDefault`: Name of the Route (must be unique in your routing table)
Content: The URL segment (try changing this to 'Content/Much/Longer/URL' and then go to http://localhost/Content/Much/Longer/URL to see how this works)
new {controller=.., action=...}: which controller/action combo to run for this route.
You could also override HandleUnknownAction in your controller:
protected override void HandleUnknownAction(string actionName)
{
return RedirectToAction("index");
}
Oh and incidentally, an extra piece of advice about routing.... if you add something to the route in braces { } these will be passed to the action as an attribute.
e.g. /Content/Much/Longer/Url/{page}
so the URL http://localhost/Content/Much/Longer/Url/999
will pass the 999 into your action, as the page attribute
public ActionResult Index(int Page) { }
I love MVC - never going back to WebForms - this is how web development should be!

Resources