Since I upgraded my application to .Net Core 3.0, I can't return a html view from a controller.
If I call View("Views/test.html"), it says it doesn't find the file. In 2.2, it worked well...
Any idea on the breaking change that made my code broken ?
If you want to be able to request static HTML files, add them under wwwroot or create a directory inside it , for example , html folder , and serve that as well via the static files middleware . Then you can return the html to view with text/html content-type :
public IActionResult Index()
{
return File("/html/test.html", "text/html");
}
Thanks.
Finally, I modifier StaticFiles Middleware configuration to serve also html files that are not in the wwwroot folder using this code :
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "MyFiles/MyAngularJSApplication")),
RequestPath = "/app",
});
see : https://learn.microsoft.com/fr-fr/aspnet/core/fundamentals/static-files?view=aspnetcore-3.0
Related
In my controller I have:
public function showIndex()
{
return View::make('index');
}
In my views folder I have index.php.
But it's not showing? Where am I going wrong?
I do not wish to use blade templating.
I also had an index.blade.php file in there. It was loading this by default. Deleted the file and now works.
I am converting an asp classic (.asp) to mvc4. The site is fine, but when I try to map the legacy pages to the new routes it fails big time.
I just want to : default.asp to / or pageA.asp to /NewPageA
When I try to map the old pages , looks like the asp classic handler intercepts first the request and try to find the page .asp, but as doesn't exist, it throws 404 error.
I am mapping wrong ? my mapping is like this:
routes.MapRoute("DefaultOld", "default.asp", new { controller = "Home", action = "IndexOld" });
routes.MapRoute("MenuOld", "menu.htm", new { controller = "Home", action = "IndexOld" });
routes.MapRoute("RecepcaoOld", "recepcao.htm", new { controller = "Home", action = "IndexOld" });
routes.MapRoute("EntradaPorOld", "entrada_por.htm", new { controller = "Home", action = "IndexOld" });
Is possible to tell the server to ignore the classic pages and let the mvc route to intercepts the request and act as it should ?
I am using IIS 7.
I tried this solution here (http://blog.abodit.com/2010/04/a-simple-redirect-route-handler-for-asp-net-3-5-routing/), works fine to map tp aspx pages, but when I try to use with asp or htm, it doesn't work at all. So I think I need t disable the handler od classic asp, is that correct ? but in the htm side, the StaticHandler if I disable it, the images and all static resources would not work well, correct ?
cheers
In the application I am working on, I have an Html page inside views folder and I have mentioned the action as follows.
<form name="form" onsubmit="return validateForm();" method="post" action="//Controllers/RegistrationController.cs">
The registration controller returns a view.
public ActionResult Detail(string name)
{
return View();
}
When I run the program, I get server not found error.
I also tried changing the action string to action="//Controllers/RegistrationController.cs/Detail"
but got the same error.
Does the action string have to be written in some other way?
Thank you very much in advance for your help.
Assuming you are using the default routes ({controller}/{action}/{id}) you need:
action="/Registration/Detail"
Actually I would recommend you using HTML helpers to generate forms and never hardcode them as you did:
#using (Html.BeginForm("Details", "Registration", FormMethod.Post, new { name = "form", onsubmit = "return validateForm();" }))
{
...
}
Description
You don't have to set the path like in your solution. You don't need to set Controllers because the framework knows that you mean the controller.
Assuming that you dont change the routing in global.asax, your RegistrationController.cs has an ActionMethod called Detail (decorated with [HttpPost]) and the following folder structure within your project.
Controllers/RegistrationController.cs
Views/Registration/Detail.cshtml
#using (Html.BeginForm("Detail", "Registration", FormMethod.Post, new { #onSubmit = "return validateForm();" }))
{
// Your Form's content
}
/registration/detail - you don't need to reference the path to the actual file. The framework finds the controller class and invokes the requested action for you. It uses the routes as defined in global.asax.cs to determine the controller and action from the url. The default route is {controller}/{action}/{id} where the first two have defaults of "Home" and "Index", respectively, and the third is optional. You can change this if you want by adding/modifying the route set up.
In my MVC project has 2 Areas which is Admin and Client and I need to dynamic config Layout for Client side, In _ViewStart (in client) file will set layout for all of client page.
Layout = "~/Views/Shared/_Layout.cshtml";
So if we need to change client layout we can change Layout path of cshtml file in _ViewStart file right? I cant find how to change inside ViewStart file or Is there another solution in this case.
Thanks for your Help :)
Remember that anything within the #{ ... } is treated as code. So, it should be a simple matter of placing a condition in there to change how it's inherited:
#{
Layout = "~/Views/Shared/_Layout.cshtml";
if (User.Current.IsAuthenticated) {
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
}
Though you're probaby better off looking at Themes (and have an admin/user theme). Alternatively, you can make your _Layout.cshtml smarter and have it handle the different views based on conditions as well.
See Also: MVC3 Razor - Is there a way to change the Layout depending on browser request?
Your question has not enough information to give you a complete code sample.
But basicly you can do this
if (InsertIsAdminLogicHere) {
Layout = "~/Views/Shared/_AdminLayout.cshtml";
} else {
Layout = "~/Views/Shared/_Layout.cshtml";
}
If you show us how you determine admin or not, we can provide more help.
hope this helps
You can take advantage of Nested Layouts. Create a base controller and drive all controllers from this one.
public class ControllerBase : Controller
{
public ControllerBase()
{
ViewBag.Theme = "~/Views/Shared/Default/Views/_Layout.cshtml";
}
}
public class HomeController : ControllerBase
{
public ActionResult Index()
{
return View();
}
}
_ViewStart.cshtml (don't make any changes in this file)
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
Views/Shared/_Layout.cshtml
This is default Layout file of Asp.NET Mvc. Empty this and replace these lines.
#{
Layout = ViewBag.Theme;
}
#RenderBody()
You can Modify this way for Areas. You can fetch active template info in BaseController from database or wherever you want.
Btw, if you want to put your views outside of ~/Views folder search for ThemeableRazorViewEngine
in Views/_ViewStart.cshtml
#{
object multiTenant;
if (!Request.GetOwinContext().Environment.TryGetValue("MultiTenant", out multiTenant))
{
throw new ApplicationException("Could not find tenant");
}
Layout = "~/Views/"+ ((Tenant)multiTenant).Name + "/Shared/_Layout.cshtml";
}
This is for an ASP.NET MVC3 web application. In RegisterRoutes I have the following as my only line:
routes.MapRoute("Default", "Configuration", new { controller = "DeviceConfiguration", action = "Index" });
When I run the project, going to the URL /Configuration/ gives me a 404 error. However if I change the word Configuration to any other word, such as:
routes.MapRoute("Default", "Configuratio", new { controller = "DeviceConfiguration", action = "Index" });
Then going to the URL /Configuratio/ loads just fine. It seems as if ASP.NET is simply refusing to route to the URL /Configuration/.
Again, this is the only line in RegisterRoutes; I've tried commenting out everything else to debug this. I have no MapRoute or IgnoreRoute calls in my code anywhere else, and I am not editing the routing table in any location.
How can I change this behavior?
I suspect that you have a physical folder called Configuration under the root of your application. The ASP.NET MVC routing engine has preference for physical folders over routes. One possible way is to set the RouteExistingFiles property to true after your route definition:
routes.MapRoute(
"Default",
"Configuration",
new { controller = "DeviceConfiguration", action = "Index"
});
routes.RouteExistingFiles = true;