I am getting the following error when debugging my header links.
I have tried recreating my Index.cshtml (Empty and with Razor/HTML) and my ServicesController.
If I browse to Services/Index explicitly then the page loads up correctly.
IE: localhost:58069/Services/ does not work but localhost:58069/Services/Index does
My other controllers are working correctly using the same structure and ActionLink helper.
Not working code:
public class ServicesController : Controller
{
// GET: Services
public ActionResult Index()
{
return View();
}
}
Action Link HTML Helper for Services/Index
<li>#Html.ActionLink("Services", "Index", "Services")</li>
Working Code
public class HomeController : Controller
{
// GET: Home/Index
public ActionResult Index()
{
return View();
}
}
Working Helper
<li>#Html.ActionLink("Welcome", "Index", "Home")</li>
Route Config
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 }
);
}
Tried the following SO solutions:
ASP MVC in IIS 7 results in: HTTP Error 403.14 - Forbidden
This behavior is caused by the Services folder (below Scripts folder) you have in your project. When hosting in IIS, you should not have a file or folder with the same name as your ASP.NET route.
Ways to solve this:
routes.RouteExistingFiles = true; will let ASP.NET handle all the routing
Rename your Services folder or controller
Move Services to a separate library and remove the folder from the web project
I'd recommend the latter option as settings RouteExistingFiles to true would need additional research to double-check you're not opening any security holes and it cleans up your web project (a little bit of separation never hurts).
Related
I have a MVC application, I try to run the application but I can not get any page in Browser, I am guessing the path has some problem.
This is RouteConfig:
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
routes.MapRoute(
name: "Default", // Route name
url: "{controller}/{action}/{id}", // URL with parameters
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
}
This is HomeController:
public ActionResult Index()
{
ViewBag.Message = "Welcome to DevExpress Extensions for ASP.NET MVC!";
return View();
}
This is the View's path:
Views -> Home -> Index.cshml
#Html.DevExpress().ReportDesigner(settings =>
{
settings.Name = "ReportDesigner";
}).Bind(new DevExpressDemo.Reports.Report1()).GetHtml()
This URL is in Project URL: http://localhost:53975/
When I run the project I go to this URL:
https://localhost:53975/Home/Index
But this is the response I am getting in Browser:
This site can’t provide a secure connectionlocalhost sent an invalid response.
When I go to http://localhost:53975/ it convert it automatically to https and shows : This site can’t provide a secure connection
You are trying to access it through https rather than http. Go to http://localhost:53975 in your browser and see if that works.
change HTTPS to HTTP on yourr brrowser address bar
For some reason my application keeps trying to find the Home Controller when redirecting back to the returnUrl (through the AccountController Login Action) even though I've changed my HomeController to be called DashboardController as well as having changed my RouteConfig.cs
namespace application
{
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 = "Dashboard", action = "Index", id = UrlParameter.Optional }
);
}
}
}
When my application starts it automatically is redirected to the Account/Login view due to an [Authorize] attribute in the controllers. But when I hand type in Account/Login into my Url the returnUrl becomes equal to "/" in the GET action, and upon signing in I receive a 404 error for missing page since it can't find HomeController Index View (which is actually DashboardController Index View).
What else do I need to change so that my application no longer searches for a Home Controller?
The solution to my problem was that the Account Controller RedirectToLocal action was redirecting to the Home Controller Index Action so I had to change it to Dashboard Controller Index Action.
private ActionResult RedirectToLocal(string returnUrl)
{
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Dashboard");
}
That controller action will typically look for the file Index.cshtml in the "~/Views/Dashboard" folder. If you did not rename the "~/Views/Home" folder to "~/Views/Dashboard", the controller will be looking in the wrong place for the file and will not be able to find it, therefore producing the 404 error.
I have this in my controller:
public ActionResult Details(int? id)
{
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Sales saleRecord = new Sales();
var result = saleRecord.GetSalesOrderHeader((int)id);
return View(result);
}
However if I browse to /ControllerName/Details/5 I get this error:
"Server Error in '/' Application. - The view 'details' or its master was not found or no view engine supports the searched locations."
The strange thing is that if I use ASP.NET Scaffolding to generate a view, the Details part of that works fine. It's basically the same code I have above as well.
Here's my RouteConfig.cs, if that's relevant:
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 }
);
}
}
So why is that the scaffolding controller works fine, without having anything specific added to RouteConfig, yet my controller Details method is not working?
There should be more detail in error that what locations have been searched. Did you put break point in controller and checked if its being hit?
try
return View((object)result);
instead of
return View(result)
cause its calling overload View(string viewName)
If this doesn't work, try to specify viewName explicitly like this:
return View("ViewName", name);
Additionally, check your folder structure. MVC looks for views (like Index) under the views folder but they also have to be under a folder named after their controller (except partial views).
There are a bunch of similar questions, but none of the answers have worked me. I'm try to return 'RedirectToAction("Index");' which is in the same controller, from 'public ActionResult Create()' when the user is not authorized to create a new widget. I have no other folders with the name Index, although there are other 'Public ActionResults Index()' functions in other controllers. Any suggestions wouls be greatly appreciated...thanks
I have tried to replicate your scenario to no avail.
New MVC5 project with a Test controller tried both Authorize attribute on both actions just to test, but all they do is actually redirect you to the login page if you are not authorized to view the resource.
HTTP 403.14 is a code for directory listing denied.
It sounds to me that you are redirecting to a URL that is not being handled by the MVC and instead is being picked by your IIS express, which by default doesn't allow listing of directory contents.
So questions to consider are:
1: what is the exact url that you are calling.
2: How is your routing setup, do you have any custom routes ? order of the routes is important.
To be able to provide you with more info please do post the controller code as well as your RouteConfig.cs file.
Here is the test code.
public class TestController : Controller
{
// GET: Test
[Authorize] // just redirects back to login page if you call create action.
public ActionResult Index()
{
return View();
}
// GET: Test/Create
public ActionResult Create()
{
return RedirectToAction("Index");
}
}
Here is the routes config file. unchanged.
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 }
);
}
from here i got a little touch about Setting an alternate controller folder location in ASP.NET MVC. here is the url Setting an alternate controller folder location in ASP.NET MVC
they guide us we can change the namespace and also specify the name space in routine code and these way we can solve it but this above link is not showing how to change or store controller related .cs files in other folder location.
suppose i want to store controller in folder called mycontroller in root then what i need to do. guide me please. thanks
UPDATE
You can do this using Routing, and keeping the controllers in separate namespaces. MapRoute lets you specify which namespace corresponds to a route.
Example
Given this controllers
namespace CustomControllerFactory.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return new ContentResult("Controllers");
}
}
}
namespace CustomControllerFactory.ServiceControllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return new ContentResult("ServiceControllers");
}
}
}
And the following routing
routes.MapRoute(
"Services",
"Services/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "CustomControllerFactory.ServiceControllers" } // Namespace
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "CustomControllerFactory.Controllers"} // Namespace
);
You should expect the following responses
/Services/Home
ServiceController
/Home
Controllers
There is no need to do anything, storing controllers in Controllers is only convention, not a requirement. Basically you can store controllers in any folder.
Check your RouteConfig.cs and you will probably find that a namespace has been defined for each route. You controller classes can be placed anywhere in the project as long as they still match the namespace defined in your routes.