MVC3 Web Application Will Not Publish - asp.net-mvc

I am trying to learn MVC 3, so I am a noob. For now, I just want to make a basic site, that is an HTML page using jQuery and CSS. While I am using MVC, I don't really need a model, since there is not really any data being passed to the application. However, this is creating a problem for me, because I am getting a HTTP 403.14 Forbidden error when I try to publish this site. I think that there is something wrong with the way the application is structured that will not allow it to execute properly when I got to localhost:1081 web site. Here is all I have:
HomeController.cs
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
}
}
This just returns the view of Index.cshtml.
Index.cshtml:
#{ViewBag.Title = "My Web Site";} <h2>Web Site Title</h2>
All of my HTML code was put into _Layout.cshtml. jQuery and CSS are used. The site works fine when I do the debug option, but when I try to publish it gives me 403.14 forbidden. I have run the inline command aspnet_regiis -i and it seemed to work, but did not allow the project to run.
Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
If I move the code in _Layout to Index, it doesn't work. Is there a way I should be linking this to Index.cshtml?
Web.config:
<system.webServer>
<defaultDocument enabled="true">
<files>
<add value="_Layout.cshtml" />
</files>
</defaultDocument>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
I do not want directory browsing, and I have tried to play with this option as well. If I enable, it allows me to see the contents but does not render the page.
So two questions: 1.) Do I need to have a model, even though I am not passing any data to the application, just trying to render a site? 2.) Is my site set up / structured properly or what exactly am I doing wrong here?
Thanks,
Nick

No, you don't need a model.
First, don't use the default document. MVC overrides the default document handling and uses the route system.
Second, you don't want to try and use the Layout page as your document. Layout is like a master page, and is used to create a wrapper around your real document.
Third, saying "will not publish" means that you are having problems actually publishing the site via the publish mechanism. Your problem is not that you can't publish, because obviously it is doing so, but that your site isn't executing.
Fourth, 403.14 means it's trying to list the contents of directory, but this shouldn't happen if MVC is configured correctly because MVC's routing takes over. This means you have a problem somewhere in the asp.net pipeline.
Where are you publishing to? Did you configure IIS to setup a site at this location? Given that you are trying to access the site from a different port number, It would seem to me that you have not setup IIS to do this and are instead trying to use the same port that's used for debugging.
In order to publish a site, IIS must be configured to use that location.

Related

Undetectable caching

I'm a web app developer with ASP.NET MVC 5 in server-side and AngularJS in client-side. The navigation, in client-side, is managed with module UI-Router. In server-side I've configured my custom authentication and authorization. When I excecute the app can see a navigation bar with many links. In particular, I have a link named "Overview" that redirects to controller Overview.
Code client (html):
<a ui-sref="Overview">Overview</a>
Code client to redirect (angular):
.config(function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("Home/Index");
$stateProvider
.state("Overview", {
templateUrl: "Home/Overview",
url: "/overview"
})
Controller code:
[OutputCache(Duration=0, NoStore=true)]
[AllowAnonymous]
public ActionResult Overview()
{
return PartialView();
}
With a breakpoint in line "return PartialView()" to controller code, I can see that controller returns partial view when I click over "overview" in menu app. But when I click second time, the breakpoint does not trigger. I think it's a caching issue.
I have read that caching issue can be generated:
* In server-side.
* In client-side.
* Even IIS.
I have tried many solutions: In server side I use attribute [OutputCache].
When i read in my browser the http headers i can see
In client side i could not find a solution to avoid caching, but i think that UI-Router shouldn't cache anything.
As additional measures I put in my web.config:
<system.webServer>
<caching enabled="false" enableKernelCache="false" />
</system.webServer>
even, I created my own custom ActionFilterAttribute but did not work.
i don't know what else to do.
PS: sorry for my english
If there is any other plugin which does caches its templates, then your solution will clean those templates and make those plugins unusable.. hence your solution is not good.
To solve problem of html caching you should add random query string at the end of html filename... the other solutions you mentioned are for non-caching of api responses and not for static content (html/js/css files)
To add random query string to ur html. either you can use modules like grunt-cache-busting or gulp-rev or use query string as param query-string-cache-busting.
Please note that datetime/file-hash are best cache-busting param
Ok, I've found the solution.
Despite avoid caching from server side with attributes like OutputCache, or avoid caching from IIS setting my web.config, the issue persisted.
AngularJS was the guilty. In other post I found the solution.
myApp.run(function($rootScope, $templateCache) {
$rootScope.$on('$viewContentLoaded', function() {
$templateCache.removeAll();
});
});
This solution is provided by Mark Rajcok and Valentyn Shybanov. So thanks to both.

Error - This type of page is not served

I have an MVC (version 5.0) application that I am running from VS 2012 with Web setting of 'Use Visual Studio Development Server'.
When the active file in VS is non .cshtml file, routing works absolutely fine.
However, if the active file is a .cshtml file and I run the application in debug mode, I get this error -
Server Error in '/' Application.
This type of page is not served.
FYI, snippet from RegisterRoutes() -
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Employee", action = "Index", id = UrlParameter.Optional }
);
What makes loading of application behave differently when .cshtml file is active/in focus in Visual Studio?
MVC is built on top of asp.net, so uses the same mechanics for serving files. Because .cshtml files are views and should be served via a controller (MVC) and not served directly, they are blocked as you have found.
You should set the url to the view as defined in the controller (eg /Employee/Index) rather than the page that is used to generate the view (eg /Views/Employee/Index.cshtml)
You can do this by changing the project settings:
right click project
properties
Web
Start URL (or Specific Page)
(it's probably set to 'Current Page')
If, for some reason, you do want to see the source code of the view, then it's blocked via the machine's web.config with this line:
<add path="*.cshtml" verb="*" type="System.Web.HttpForbiddenHandler" validate="True"/>
I recommend you don't change this.

Piranha CMS routing issue with ASP.NET MVC

I'm in the process of trying to integrate Piranha CMS (v2.2.0) with an existing ASP.NET MVC application. I can run all the original application pages, and the CMS manager pages. I can also see drafts of pages managed by the CMS, but when I try to view the live page version hosted from the CMS I get a HTTP 404 "The resource cannot be found" message.
So the following draft url works:
http://localhost:5316/draft/start
But the following live url fails:
http://localhost:5316/home/start
The original application does have a "Home" controller which I have tried renaming to "Test" in case of conflict issues. I could see the new "Test" located content, but the /home/start url still failed.
As advised, my RouteConfig code is:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "Maps.Portal.Controllers" }
).DataTokens["UseNamespaceFallback"] = false;
And my web.config settings are:
<settings>
<managerNamespaces value="" />
<disableManager value="false" />
<passiveMode value="true" />
<prefixlessPermalinks value="false" />
</settings>
I have tried setting the prefixlessPermalinks setting to true but this didn't help.
I'm guessing that Piranha CMS isn't catching the routes for pages hosted with itself? any ideas?
By looking at your config I can see that you have followed the guidelines for setting up Piranha CMS for an existing project by setting passiveMode to true. Let me clarify a bit what this parameter does.
Passive mode is used for applications where you only want to use Piranha CMS as a backend content store and not handle any routing. This means that this parameter effectively turns off all url's to permalinks in the system to not interfere with the existing routes of the application.
If you want to mix your existing application controllers with pages solely generated by Piranha CMS you have to set passiveMode to false which will make the routing for permalinks active again.
Once this is done you will be able to access your pages with or without prefixless permalinks.
Regards
HÃ¥kan

Invoke ASP.NET MVC Controller When Requesting .html file

I need to add some new life to a legacy application :)
I'd like to call an MVC controller when a "static" HTML page is requested in order to add some markup to the page before returning it to the client.
I tried to follow the approach found in this thread: How to read web.config settings in .html page?
...but even though I have this route defined:
routes.MapRoute(
name: "Topic",
url: "html/{fileName}.html",
defaults: new { controller = "Topic", action = "Index" });
the controller is not being called. I have my web.config defined with:
<remove name="WebServiceHandlerFactory-Integrated" />
<add name="HTML" path="*.html" verb="*"
type="System.Web.UI.PageHandlerFactory"
resourceType="File" preCondition="integratedMode" />
I suspect that I need to call something else besides the PageHandlerFactory or perhaps the issue is something entirely different.
UPDATE: My dev environment is working with integrated pipeline mode, but I need to check if my production environment will support it.
If you do this:
routes.RouteExistingFiles = true;
You should find this works - even without the handler addition. In the controller you can load the HTML directly using the HostingEnvironment.VirtualPathProvider's GetFile method and do something with it - or better still just use a normal MVC view that renders the same content as the static file, just with your additions.
Although be aware that this means any files that are potentially caught by any routes will be pushed into the MVC pipeline. This isn't generally a concern, however, if decent separation of routes and physical paths is used.
I setup the same situation as you and it worked well for me, so you have the key components in place. Some things to keep in mind for the testing and troubleshooting:
Your web.config does need the build provider for the html extension:
<system.web>
<compilation>
<buildProviders>
<add extension=".html"
type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
</compilation>
</system.web>
A copy and paste of your handlers works for me, so that looks good.
And a copy and paste of your MapRoute works for me too, although I used the default Home controller in a clean project. So as a double check just confirm that you have a controller called Topic with an ActionResult method called Index().
And make sure that your url is localhost.com:{port}/html/test.html with the /html/ in the path since your rule asks for that.
Another good test is to change your MapRoute to use aspx instead and test an aspx page and see if that works. That will confirm whether or not it's the IIS mappings or if it's the MVC rules. If it works with aspx then the issue is related to the handler, but if it fails with aspx too then it's something with MVC.
Also confirm that you're using IIS Express and not Cassini. Cassini will not handle that correctly, but IIS Express will. You can confirm by right-clicking on your project and you should see a menu option called "Use Visual Studio Development Studio...". That will only exist if you are currently using IIS Express.

ASP.NET MVC2 Routing IIS6 - only default routes work

I've deployed an ASP.NET MVC2 website on a Windows Server 2003 machine running IIS 6.
I'm using pretty much the default routing in a standard MVC project:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Products", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
}
Navigating to http://localhost/MyApplication takes me to the List page just fine. Navigating to http://localhost/MyApplication/Products/Details/21 gives me a 404. This routing worked fine on the inbuilt development server in VS2010.
I've put in the standard IIS wildcard aspnet_isapi.dll mapping mentioned all over the place - navigating to the List page didn't work before I did - but navigating to anything other than the default route is broken.
I'd really like to keep my extensionless URLs. Does anyone have any idea as to why the routing would work for the default webpage, but no others?
*Edit: just tried adding the .aspx extension, ie now my route looks like this:
routes.MapRoute(
"Default", // Route name
"{controller}.aspx/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
And it has the same behaviour, except this time I get the asp 404 page instead of the html 404 page...
*Edit 2: tried it again using the following route and making sure .mvc was mapped to aspnet_isapi.dll:
routes.MapRoute(
"Default", // Route name
"{controller}.mvc/{action}/{id}", // URL with parameters
new { controller = "Downtime", action = "List", id = UrlParameter.Optional } // Parameter defaults
);
This time I got an 'Internet Explorer cannot display the webpage' 404-style page. I've now got 3 different 404 errors from using these 3 different methods...
*Edit 3 Return of the Edit: I have the site running in IIS 5.1 on Windows XP professional with only a wildcard remapping on the virtual directory, but heaven forbid I can get it to run on the webserver...
First of all, you should follow this walk-through: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx
(just to make sure you've not missed something). This shows how to get the extension-less and extention'd versions working. You might want to at least check whether the extension'd version works to check that other things aren't misconfigured.
After that, perhaps you should try adding something like this..
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
... as it can really help in determining where your routes are broken.
Some other things to check:
Is IIS configured with ASP.NET? Check properties of the virtual directory. On the VDir select the configuration button and check that the usual ASP.NET extensions are mapped to the .NET 2.0 ISAPI dll. This will be something along the lines of
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll.
Is ASP.NET 2 enabled on IIS (under IIS 6 this is off by default): in IIS manager check the Web Service Extensions folder to enable it.
Have you deployed global.asax?
Found it - the routing is actually working, my URLs were not. I was using javascript to build some of the URLs and it turns out that I wasn't linking to
http: //localhost/MyApplication/Controller/Action/ID
I was actually linking to
http: //localhost/Controller/Action/ID
Building the links like this was working on the development server, but once the site was deployed to a virtual directory on the webserver those addresses are incorrect because of the extra application name in the URL.
In conclusion, be careful with your URLs - don't build them out of strings like I did.

Resources