host MVC app inside a website - asp.net-mvc

I have a website (not a web application- in visual studio you get two options-create a website/project) running on IIS 6.0. Now I want to develop few features in MVC architecture.
So I created one MVC application in visual studio and everything is working fine on localhost as a separate application. Now I want to host this MVC app also inside the website I have already deployed.
I created a virtual directory(MVCDir) inside the default website in IIS 6.0. The global.asax file which was in root folder I added the routing function-
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.Ignore("{resource}.axd/{*pathInfo}")
routes.Ignore("{resource}.aspx/{*pathInfo}")
routes.MapPageRoute("Default4", "{controller}/{action}/{id}", "~/MVCDir", False, New RouteValueDictionary(New With {.controller = "Home", .action = "Index", .id = Mvc.UrlParameter.Optional}))
End Sub
*** NOTE- If I write routes.ignoreRoute instead of routes,ignore it says- IgnoreRoute is not a member of System.Web.RoutingCollection*
I called this routing function inside application_start function
now when I run domain.com/home/index
How to solve this problem?
it says resource not found

Maybe this is a problem with you child application inheriting configuration from parent. I had a similar problem when trying to put an app under a website.
You'll need to wrap <system.web> in <location> tag with inheritInChildApplications="false"
Example:
<location path="." inheritInChildApplications="false">
<system.web>
<!-- ... -->
</system.web>
</location>
Here you have more detailed explanation.

Related

Adding MVC application as a subsite

I have a generic web application up and running http://sites.acme.com.
I want to add a SubSite http://sites.acme.com/subsite1.
In the past to add a new subsite (SubSite1), we would simply publish the project from VS into a sub folder of that folder Sites/SubSite1, and edit the web.config of Sites by adding:
<location path="SubSite1">
<system.webServer>
<defaultDocument enabled="true">
<files>
<clear/>
<add value="index.aspx"/>
</files>
</defaultDocument>
</system.webServer>
</location>
Now, trying to publish an mvc website, that doesn't have aspx pages, I'm trying to figure out how to get IIS to recognize that it needs to hit the SubSite1.dll in order to start the home controller, and get the index.cshtml to return.
Do I need to edit any configuration in the sites' web config?
Typically in case of MVC application we don't set default document. We set default controller and default action when we register route. For example following route registration sets default controller as "Home" controller and default action as "Index".
routes.MapRoute(
"Default", // Route name
"{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
So when user types http://mysite/ then it evokes "Index" action in "Home" controller. It is as good as user typing http://mysite/Home/Index
Now, in order to setup your scenario you don't need to add default document configuration as shown in question. Just configure "subsite1" as "application" in IIS under "sites" application and you are good to go provided you have properly setup route for subsite1.

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.

MVC3 Web Application Will Not Publish

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.

Resources