Have ServiceStack default route go to MVC3 Controller - c#

I've installed ServiceStack.Host.MVC into an existing MVC3 project that has a lot of controllers.
It's now routing to default.htm. I found another StackOverFlow question that said to change the Default redirect path, do the following.
SetConfig(new EndpointHostConfig {DefaultRedirectPath = "/Foo" });
But I really want to change it to execute the default Controller and Action as setup in the global.ascx. I've edited the DefaultRedirectPath to point at /Home, and it seems to work fine, and doesn't add Home to my url or anything, but I'm not 100% on the relationship between having an AppHost in my application.
If changing this isn't the right thing to do, what's the best way to get started integrating some Service Stack AppHost in with a regular MVC application. I'm going to move my base controllers to ServiceStackController, and slowly start using the new Session and User management stuff. Eventually, I'm going to want to use the authorization or shared sessionId with some services, but I'll get to that later.
To start, my goal is to start using some of the IOC functionality of Funq.

You probably don't need DefaultRedirectPath, because it is just a redirect to a predefined URL. You need to have your MVC controllers hit when using your old urls, and have a new URL area for ServiceStack. To do this, start by setting ServiceStackHandlerFactoryPath. It should allow you to host ServiceStack in a sub-url, and it will not interfere with your existing controllers, unless there is a conflict in the path:
SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "api" });
The ServiceStack http handler in your web.config will need to match:
<location path="api">
<!-- Required for IIS 7.0 -->
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
</location>

The ServiceStack BootstrapAPI application sets the CatchAllController to be the HomeController of the MVC application. This worked for me.
In the AppHost::Configure, I put the following code:
public override void Configure(Funq.Container container)
{
// Other Configuration constructs here
ServiceStackController.CatchAllController = reqCtx => container.TryResolve<HomeController>();
}

Related

.net core 2.0 app blocks plesk-webstat

I published an aps.net core 2.0 mvc app to a shared web hosting server that uses Plesk as control panel. The app works fine. However, I got the following error message when trying to access the web statistics:
This example.com page can’t be found
No webpage was found for the web address: https://example.com/plesk-stat/webstat/
HTTP ERROR 404
I contacted their support and got the answer "the .net core application settings aren't allowing the webstats to load. We recommend you consult with an experienced website developer to customize the web.config code accordingly for the website.", but they don't know how to configure the web.config file.
I really want to make the webstat to work. Any suggestion will be appreciated.
If URL Rewrite is blocking the access, try adding this string to the <conditions> section of the rule which is affecting webstat page:
<add input="{REQUEST_URI}" pattern="^/(plesk-stat/webstat)" negate="true" />
If that does nor help, configure failed request tracing to find which exact module is performing a redirect.
Along with changes in the web.config of the ASP.Net Core site itself to send the /plesk-stat/ url to IIS, a web.config must be added in the following directory:
C:\Inetpub\vhosts\{domain.tld}\.plesk\statistics\{domain.tld}\
(replace {domain.tld} with your domain), with the following contents:
<configuration>
<system.webServer>
<handlers>
<remove name="aspNetCore" />
</handlers>
</system.webServer>
</configuration>
This has to be done by the hosting provider on the server. Maybe you should contact the support of your hosting provider.

Minimalist Web API Attribute Routing Setup - Help on Shared Hosting

I have a SUPER simple Web API. It is openly non-secure and simply provides free data. I'd like to move it from my personal server to shared hosting (e.g.: GoDaddy), but I'm having issues.
There are a lot of similar questions relating to 404 status when attempting to access Attribute Routing Web API services on shared hosting solutions. But none of them have worked for me. This is my SUPER Simple current setup:
Create a new .Net Framework 4.5 EMPTY C# Web Application named YourApplicationName
Ensure all checkboxes and packages are NOT Checked
Add a New "Global.asax" object
Replace the entire contents of the C# with the following:
using System.Web.Http;
namespace YourApplicationName
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start()
{
System.Web.Mvc.AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure((HttpConfiguration config) => { config.MapHttpAttributeRoutes(); });
}
}
public class TestController : System.Web.Http.ApiController
{
[System.Web.Http.Route("api/Test")]
public string Get()
{
return "Test Info";
}
}
}
Replace the Web.config file with the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<customErrors mode="Off" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
</configuration>
Delete ALL References other than System and System.Web
PM> Install the following two packages:
Install-Package Microsoft.AspNet.Mvc
Install-Package Microsoft.AspNet.WebApi.WebHost
With that, I can run in Dev, publish to my personal IIS, run on my company's IIS Servers, or even publish to azure (http://suamereapi.azurewebsites.net/api/Test) and if I navigate to the route, I appropriately get back "Test Info".
But when I publish to my shared hosting, I receive a 404 status. Note that mvc controllers that return Views work just fine in the shared hosting, just not the ApiControllers.
Possible Issue 1 is that the IIS pipeline is not set up as Integrated on the hosted account. Of course I have checked that mine is set to Integrated.
Possible Issue 2: Moreso related to API's NOT using Attribute Routing, is that the Register Routes needs to come before the GlobalConfiguration. This is not applicable in a pure Attribute-Routing Web API setup.
Possible Issue 3: Some of the DLL's aren't in the shared hosting GAC, and also are not published from your local setup because they are not Copy Local. I have altered Copy-Local to multiple setups from none, to just what is required, to all references.
Possible Issue 4: Shared hosting messes with the configuration paths because of virtual directories for subdomains or what not. That is fixed by adding a rewrite to the Web.config as such:
rewrite>
<rules>
<rule name="Remove Virtual Directory">
<match url=".*" />
<action type="Rewrite" url="{R:0}" />
</rule>
</rules>
</rewrite>
Possible Issue 5: Some say to fix the issue you need to add runAllManagedModulesForAllRequests="true" and work with the httpHandlers to remove and manually add extension handlers, and UrlRoutingModules.
I have done all combinations of these workarounds, but I can't imagine what the true issue is. Please let me know what I might be missing, I've spent days on this.

Deploy REST service without access to IIS

I have developed a .NET REST web service in C#. While I have plenty of C# experience, I unfortunately do not have much understanding in deploying such a service in a web hosting environment.
Due to the environment, I do NOT have access to IIS.
The advice I have been provided with by the support services of the hosting provider is as follows:
Create a subdomain of the main domain to achieve a dedicated application pool (this a requirement of the host provider)
Create a Bin folder to hold my compiled libraries of source code
Add the following to the Web.Config file:
<system.web>
<httpHandlers>
<add type="ReportRESTWebService.Service, ReportRESTWebService" verb="*" path="report" />
</httpHandlers>
</system.web>
<system.webServer>
<handlers>
<add name="report" path="report" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="File" requireAccess="Script" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
</handlers>
<directoryBrowse enabled="false" />
</system.webServer>
The above would have the effect of creating a handler mapping for the report resource on all HTTP verbs and forwarding any HTTP traffic on that resource to my ReportRESTWebService.dll for handling.
The point I am unclear on is whether the above will be satisfactory and how do I test whether the advice I am given is correct. I know that I have the site running locally but I have access to IIS so I have control over the configuration.
Hopefully somebody can help.
Thanks
If you are using Wcf Rest,then you can probably consider hosting it as as windows service
or self hosted service.
Windows Service
http://blogs.msdn.com/b/juveriak/archive/2009/03/15/rest-endpoint-hosted-in-a-wcf-windows-service.aspx
Self hosted Service
http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/self-hosted-wcf-rest-service-or-hosting-wcf-rest-service-in-console-application/
It would seem, after a day of exhaustive testing, that the steps I had taken (detailed in the question) would be satisfactory.
One point to watch out for is the matching Managed Pipeline Mode for your application pool. Failure to match this up correctly with your Web.Config will result in pain.

REST with .NET using a Default Handler

I am creating a REST service using .NET and have used a generic handler, Default.ashx, to handle the incoming request. This allows me to access the service using methods like "http://rest/test" without issue. But when a file extension is added IIS no longer redirects the request but instead looks for a file. How can the web.config be modified so that URL request like "http://foo/test.xml" and "http://foo/test.json" are also handled by the DefaultHandler.ashx? I have done this before so know its possible but cannot remember the configuration.
You can see an example of this in my HttpClone app's web.config. The gist of it involves removing handlers for the extensions you don't want like this:
<system.webServer>
...
<handlers accessPolicy="Read, Script">
<clear />
<add name="Favorite-Icon" path="/favicon.ico" verb="GET,HEAD" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="HttpClone" path="*" verb="GET,HEAD,POST,DEBUG" type="Namespace.MyCustomHandler, AssemblyName" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
</handlers>
This says to use a custom handler for all requests for all URIs except '/favicon.ico'. If you still need to use the 'Default.ashx' then you will need to get handler for the ashx type and map it to handle all paths just like this. Generally though there is no need for the ashx extension handler, just implement IHttpHandler in any assembly and reference it in the 'type' attribute above.
Note that the cassini web server (the test server in VStudio) will not map the default directory '/' to you're handler. To fix this for cassini you need a default.aspx document to exist (although it can be empty).
Note 2 - The configuration above is for integrated mode only, for classic mode the concept is the same but the settings are in a different location.

How to pass all requests to handler using Visual Studio Development server?

I've got an empty ASP.NET project with single HttpHandler, saved in Index.ashx. I want all requests to go through this,
public void ProcessRequest(HttpContext context)
I've modified my project project properties so that it loads that handler by default. However, if I type a different Url in the browser it won't be passed through that handler. How do I get it to do that?
What you want to do is generally achieved via HttpModule and not via HttpHandler. The module can intercept all requests and modify request/responses as per need.
...and to explicitly answer my question, you can add something like this to your web.config file (which can also be done through the IIS Manager GUI):
<configuration>
<system.webServer>
<handlers>
<add name="RequestHandler" path="*" verb="*" type="WebApplication6.RequestHandler" />
</handlers>
</system.webServer>
</configuration>
where "name" is an arbitrary name and "type" is the fully-qualified class name (with namespace).

Categories

Resources