Simple Web API Controller returning 404 - asp.net

I just created a basic Web Controller in my project. I hit debug and try to browse to /api/duedate and I get a 404. I am new to controllers and have been looking at every tutorial I can find. None of them say I need to add anything more to get this to work.
Imports System.Net
Imports System.Web.Http
Public Class DueDateController
Inherits ApiController
' GET api/duedate
Public Function GetValues() As IEnumerable(Of String)
Return New String() {"value1", "value2"}
End Function
' GET api/duedate/5
Public Function GetValue(ByVal id As Integer) As String
Return "value"
End Function
' POST api/duedate
Public Sub PostValue(<FromBody()> ByVal value As String)
End Sub
' PUT api/duedate/5
Public Sub PutValue(ByVal id As Integer, <FromBody()> ByVal value As String)
End Sub
' DELETE api/duedate/5
Public Sub DeleteValue(ByVal id As Integer)
End Sub
End Class
My web.config looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="webPages:Version" value="2.0"/>
</appSettings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
<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>

I believe you need to add a route.
Routing in VB
If you read the last comment in this thread it should show you how to add routing to your app.

To help anyone else out and to simplify having to read through that long post. You need to create and modify your global.asax file to include this:
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
RouteTable.Routes.MapHttpRoute("WebApi1",
"api/{controller}/{id}",
defaults:=New With {.id = System.Web.Http.RouteParameter.Optional})
End Sub

Related

Generic Handler for Site Root

I'm trying to get a handler to be called for the site root request by the browser, i.e. http://my.example.com. Given the code below, if I call /Test, the handler works as expected, but without that, I get the HTTP Error 403.14 - Forbidden (directory browsing isn't allowed).
Windows Server 2012-R2 / IIS 8.5
There is no MVC involved
ScriptModule-4.0 module is inherited so extensionless works
Similar to this question from 2012 that was never properly answered
Generic handler is given as an example...could also be a Soap Web Service
I've tried various combinations of slashes and asterisks for the handler path without success.
Generic handler:
Public Class Test
Implements IHttpHandler
Public Sub ProcessRequest(Context As HttpContext) _
Implements IHttpHandler.ProcessRequest
With New StringBuilder
.AppendLine("<html>")
.AppendLine("<head>")
.AppendLine("<title>Test</title>")
.AppendLine("</head>")
.AppendLine("<body>")
.AppendLine("<p>Hello World</p>")
.AppendLine("</body>")
.AppendLine("</html>")
Context.Response.Write(.ToString)
End With
End Sub
End Class
...and in web.config I have the following:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation strict="false" explicit="true" debug="true" targetFramework="4.5.2" />
<customErrors mode="Off" />
<authentication mode="Windows" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<system.webServer>
<handlers>
<add verb="*" name="Test" type="MyApp.Test" path="Test" />
</handlers>
<defaultDocument enabled="true">
<files>
<clear />
<add value="Test" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
The solution I came up with, but I'm open to other ideas.
In web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation strict="false" explicit="true" debug="true" targetFramework="4.5.2" />
<customErrors mode="Off" />
<authentication mode="Windows" />
<httpRuntime targetFramework="4.5.2" />
<!-- Required for Web Services via Handlers -->
<webServices>
<protocols>
<add name="HttpGet" />
<add name="HttpPost" />
</protocols>
</webServices>
</system.web>
<system.webServer>
<handlers>
<add verb="GET,POST" name="Test" type="MyApp.Test" path="Test" />
</handlers>
<modules>
<add name="AppModule" type="MyApp.AppModule" />
</modules>
<defaultDocument enabled="false" />
<directoryBrowse enabled="false" />
</system.webServer>
</configuration>
And then added the AppModule class where I evaluate HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath and do a HttpContext.Current.RewritePath so the handler defined above will pick it up.
my.example.com
my.example.com/AnyFolder/MyApplication
Matching for "~/" works if the web app is at the site root in IIS or is set up as an app within a site:
Public Class AppModule
Implements IHttpModule
Friend WithEvents WebApp As HttpApplication
Public Sub Init(ByVal HttpApplication As HttpApplication) _
Implements IHttpModule.Init
WebApp = HttpApplication
End Sub
Private Sub WebApp_BeginRequest(sender As Object, e As EventArgs) _
Handles WebApp.BeginRequest
With HttpContext.Current
If .Request.AppRelativeCurrentExecutionFilePath = "~/" Then .RewritePath("~/Test")
End With
End Sub
Public Sub Dispose() _
Implements IHttpModule.Dispose
Throw New NotImplementedException()
End Sub
End Class

ASP.NET Webforms Module, context user is null

When a video request is handled by the HTTP Module (code below) such as /website/uploads/Video/M2U00001_2.mp4 the _context.User is null.
When I run this in VS2010 on my local machine using the Visual Studio Development server _context.User is set. After I deploy to IIS 7 (.net 4.0) _context.User is Null.
'_context.User' is not Null when the http module processes an aspx page but it is Null when processing javascript, images, videos or CSS.
Can anyone explain why _context.User is null and possible solutions that will ensure _context.User is not null.
public Class VideoSecurityModule
Implements IHttpModule
Private WithEvents _context As HttpApplication
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
Dim myUserManager As UserManager
Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init
_context = context
myUserManager = New UserManager
End Sub
Public Sub OnAuthorizeRequest(ByVal source As Object, ByVal e As EventArgs) Handles _context.PostAuthenticateRequest
Const networkAuthenticationRequiredStatusCode As Integer = 511
Try
If IsVideoUrl() Then
If _context.User Is Nothing Then
LogManager.WriteMessage("_context.User is nothing:", "")
End If
Dim userId As Integer = myUserManager.GetUserIdByUserName(_context.User.Identity.Name)
If (UserRequiresAuthorization(userId)) Then
If Not UserIsAssignedToCourseContainingVideo(userId) Then
LogAccessDeniedMessage()
_context.Response.StatusCode = networkAuthenticationRequiredStatusCode
_context.Response.ClearContent()
_context.Response.Write("UnAuthorized User")
_context.Response.End()
End If
End If
End If
Catch ex As Exception
LogManager.WriteException(ex, "")
End Try
End Sub
End Class
To ensure that _context.User is set add the runAllManagedModulesForAllRequests="true" to the modules section of the web.config like below.
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="ScriptModule" />
<remove name="RadUploadModule" />
<remove name="RadCompression" />
<remove name="VideoSecurityModule" />
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add name="RadUploadModule" type="Telerik.Web.UI.RadUploadHttpModule" preCondition="integratedMode,runtimeVersionv2.0" />
<add name="RadCompression" type="Telerik.Web.UI.RadCompression" preCondition="integratedMode,runtimeVersionv2.0" />
<add name="VideoSecurityModule" type="LMS.VideoSecurityModule"/>
</modules>

wCFService returning xml instead of expected json

I was trying to make a webservice call to return json formatted data to populated a grid control. It was not working and after using fiddler and firebug to monitor the call I see the data wrapped as xml. I tried to different calls; one makes a call to mongodb and the result is a simply collection and the other is data from another endpoint that is json format. I have the webservice set up as follows:
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Services
Imports System.Web.Script.Serialization
Imports System.Net
Imports System.IO
Imports System.Xml
Imports Newtonsoft.Json
Imports System.ServiceModel
Imports MongoDB.Driver
Imports MongoDB.Bson
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ServiceBehaviorAttribute(IncludeExceptionDetailInFaults:=True)>
<ToolboxItem(False)> _
Public Class WebService1
Inherits System.Web.Services.WebService
Private mongo As MongoServer = MongoServer.Create()
Private Function convertToJson(ByVal username As String)
Dim product As New splnkObject()
product.userName = username
Dim jsonT As String = JsonConvert.SerializeObject(product)
Return jsonT
End Function
<WebMethod()> _
<ScriptMethod(UseHttpGet:=True,
XmlSerializeString:=False, ResponseFormat:=ResponseFormat.Json)> _
Public Function getDBData() As String
Dim response As String = String.Empty
mongo.Connect()
Dim db = mongo.GetDatabase("nodetest1")
Using mongo.RequestStart(db)
Dim collection = db.GetCollection(Of BsonDocument)("usercollection").FindAll()
response = collection.Collection.ToString
response = "{""d"":" + response + "}"
Return collection.ToArray.ToJson
End Using
End Function
This is the response captured in fiddler and the json tab says invalid json in body:
string [ xmlns=http://tempuri.org/ ]
[{ "_id" : ObjectId("52d2f2b3c60804b25bc5d2ca"), "username" : "testuser1",
"email" : "testuser1#testdomain.com" },
{ "_id" : ObjectId("52d2f2f9c60804b25bc5d2cb"), "username" : "testuser2",
"email" : "testuser2#testdomain.com" },
{ "_id" : ObjectId("52d2f2f9c60804b25bc5d2cc"), "username" : "testuser3",
"email" : "testuser3#testdomain.com" }]
My webconfig file as follows:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="connectionString2" value="Server=localhost:27017"/>
</appSettings>
<connectionStrings>
<system.web>
<authentication mode="None" />
<authorization>
<allow users="?" />
</authorization>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Linq, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
</assemblies>
</compilation>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx"
type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false"
multipleSiteBindingsEnabled="true" />
<services>
<service name="WbTest.Service1">
<endpoint address="" behaviorConfiguration="WbTest.Service1AspNetAjaxBehavior"
binding="webHttpBinding" contract="WbTest.IService1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
<enableWebScript />
</behavior>
<behavior name="WbTest.Service1AspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings />
<client />
</system.serviceModel>
</configuration>
The javascript call:
var myStore = new Ext.data.Store({
model: 'User',
proxy: {
type: 'ajax',
url: 'WCFService/WebService1.asmx/getDBData',
contentType: 'application/json; charset=utf-8',
reader: {
type: 'json',
root: '_id'
}
}
});
myStore.load();
Please could someone take a look and identify where the issue is.
I'm not going to say this is the "right" way, however, one option would be to not specify a return type on the method and write directly to the response(HttpContext.Current.Response) object.
<WebMethod()> _
Public Sub getDBData()
Dim response As String = String.Empty
mongo.Connect()
Dim db = mongo.GetDatabase("nodetest1")
Using mongo.RequestStart(db)
Dim collection = db.GetCollection(Of BsonDocument)("usercollection").FindAll()
response = collection.Collection.ToString
response = "{""d"":" + response + "}"
Dim responseJson as String
responseJson = Collection.ToArray.ToJson
HttpContext.Current.Response.Write(responseJson)
End Using
End Sub
Additionally, If you are going to use Newtonsoft to manipulate objects, i find this method works well.
I should note, that asmx web services are legacy and the newer technology is wcf.

"ServiceHost only supports class service types"

For some reason I can't get past this error. I've done searching, but can't find any vb examples. What am I doing wrong with this web.config?
SalesTracking.svc
<%# ServiceHost Language="VB" Debug="true" Service="SalesTracking.ISalesTracking" CodeBehind="SalesTracking.svc.vb" %>
SalesTracking.svc.vb
Imports GlobalDir
Imports System.Web.Script.Serialization
Imports System.Reflection
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Threading
Imports System.ServiceModel.Activation
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class WebService
Implements ISalesTracking
Public Function GetDataUsingDataContract(ByVal composite As ClientSideData) As ClientSideData Implements ISalesTracking.GetDataUsingDataContract
End Function
End Class
Web.config
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings>
<add name="dbconstring" connectionString="Data Source="" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<authentication mode="Windows" />
<authorization>
<allow roles="Domain Users" />
<deny users="*" />
</authorization>
<customErrors mode="Off"/>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="SalesTracking.WebServiceBehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<!-- start addition -->
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior" >
<enableWebScript/>
</behavior>
</endpointBehaviors>
<!-- end addition -->
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service behaviorConfiguration="SalesTracking.WebServiceBehavior"
name="SalesTracking.WebService" >
<endpoint address="localhost" behaviorConfiguration="ServiceAspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="NewBinding0" name="SalesTracking.WebService"
contract="SalesTracking.ISalesTracking">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Service attribute must be the implementation, not interface.
<%# ServiceHost Language="VB" Debug="true" Service="SalesTracking.WebService" ....

Making problem in Health Mornitoring of asp.net

I created a File Cleanup Provider that is inherits system.web.management.webeventprovider.
Public Class FileCleanupProvider
Inherits System.Web.Management.WebEventProvider
Private Const StateFIleFolderPath As String = "StateData/"
Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection)
MyBase.Initialize(name, config)
End Sub
Public Overrides Sub Flush()
' not required
End Sub
Public Overrides Sub ProcessEvent(ByVal raisedEvent As System.Web.Management.WebBaseEvent)
Dim DateTimeRaised As DateTime = raisedEvent.EventTime
' Remove files
Dim FilePath As String = IO.Path.Combine(HttpRuntime.AppDomainAppPath, StateFIleFolderPath)
For Each FileName In IO.Directory.GetFiles(FilePath)
If (DateTimeRaised - IO.File.GetCreationTime(FileName)) > TimeSpan.FromHours(6) Then
IO.File.Delete(FileName)
End If
Next
End Sub
Public Overrides Sub ShutDown()
'Clean up on shut down
Dim FilePath As String = IO.Path.Combine(HttpRuntime.AppDomainAppPath, StateFIleFolderPath)
For Each FileName In IO.Directory.GetFiles(FilePath)
IO.File.Delete(FileName)
Next
End Sub
End Class
And I configure in web.config this
<healthMonitoring enabled="true" heartbeatInterval="5">
<providers>
<add name="FileCleanupProvider" type="System.Web.Management.WebEventProvider"/>
</providers>
<profiles>
<remove name="Default"/>
<add name="Default" minInstances="1" maxLimit="Infinite" minInterval="00:01:00" custom=""/>
</profiles>
<eventMappings>
<remove name="Heartbeats"/>
<add name="Heartbeats" type="System.Web.Management.WebHeartbeatEvent,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a" startEventCode="0" endEventCode="2147483647" />
</eventMappings>
<!--<eventMappings>
<add name="FileCleanupEvent" type="System.Web.Management.WebHeartbeatEvent" startEventCode="0" endEventCode="2147483647"/>
</eventMappings>-->
<rules>
<clear/>
<add name="FileCleanupRule" eventName="Heartbeats" provider="FileCleanupProvider" profile="Default"/>
</rules>
</healthMonitoring>
But I got one problem that.
How can I solve that?
You need to specify your class in the configuration:
<add name="FileCleanupProvider" type="YourNameSpace.FileCleanupProvider,AssemblyName,Version=0.0.0.0,Culture=neutral,PublicKeyToken=PublicKeyHere"/>
The .NET is creating instance of the class on the fly, so the class must be "valid".

Resources