All of a sudden my app won't display a page and I now get "You do not have permission to view this directory or page." I looked at this but the suggestions there didn't help.
for my code:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
// NOTE: If this class is renamed, remember to update the global.asax.cs file
public class Service
{
[WebGet(UriTemplate = "")]
public string GetTest()
{
return "Windward update REST service running.";
}
}
When I request an aspx page, it works.
Global.asx.cs:
public class Global : HttpApplication
{
private static readonly ILog log = LogManager.GetLogger(typeof(Global));
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes();
log4net.Config.XmlConfigurator.Configure();
log.Info(string.Format("Update REST server started, running under user {0}", WindowsIdentity.GetCurrent().Name));
}
private void RegisterRoutes()
{
// The class providing the REST service
RouteTable.Routes.Add(new ServiceRoute("Service", new WebServiceHostFactory(), typeof(Service)));
}
web.config (relevant parts):
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
Nothing substantive has changed. So why do the requests no longer call the service?
thanks - dave
Related
I have a .NET Framework 4.6.1 solution which has this global.asax.cs:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{ Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = EnvironmentHelper.InstrumentationKey;
HttpConfiguration config = GlobalConfiguration.Configuration;
GlobalConfiguration.Configure(WebApiConfig.Register);
}
protected void Application_Error(Object sender, EventArgs e)
{
_telemetry.TrackException(Server.GetLastError());
}
}
This WebApiConfig.cs:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Services.Add(typeof(IExceptionLogger), new InsightsExceptionLogger());
}
}
This logger class:
public class InsightsExceptionLogger : ExceptionLogger
{
public override void Log(ExceptionLoggerContext context)
{
if (context != null && context.Exception != null)
{
var ai = new TelemetryClient();
ai.TrackException(context.Exception);
}
base.Log(context);
}
}
And this is an example of a controller and method:
public class SomeController : ApiController
{
[HttpPost, Route("api/v1/Something")]
public async Task<IHttpActionResult> Something()
{
The problem is I'm not getting any requests logged in Insights at all.
What do I need to do to get these API calls logged in Insights? (Assuming that simply adding .TrackRequest() is not necessary.)
Create a Request Telemetry initializer class and add the below code. On request Telemetry property you can select which property u want to add in a application insights.
After the Request Telemetry initializer class created add
<Add Type="webapptostoreloginappinsights.ReqTelemetryInitializer,webapptostoreloginappinsights"/>
in a applicationinsights.config file under telemetry processors
Make sure to call the request Telemetry initializer class in a global.asax.cs
Try to run the code now able to view the api request in a Application Insights
What I ended up doing was this....
Add packages:
<package id="Microsoft.ApplicationInsights" version="2.18.0" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.18.0" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.18.0" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.Web" version="2.18.0" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.WindowsServer" version="2.18.0" targetFramework="net472" />
<package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.18.0" targetFramework="net472" />
<package id="Microsoft.AspNet.TelemetryCorrelation" version="1.0.8" targetFramework="net472" />
Ensure web.config <system.web> contains this:
<httpModules>
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
During the process the ApplicationInsights.config file was created and once deployed the application started logging Reuqest entries in Insights.
I have a web api in Nancy 1.4.3. I have defined some settings in web.config under applicationSettings section. I was wondering how can I read these settings in a Nancy module (or Bootstrapper)? Because the conventional ways of reading these settings as in MVC/WebAPI are not available in Nancy.
Please consider that I am using Nancy 1.4.3 not Nancy 2x and .net 4.6.1 not .net core.
For simplicity, I am writing how the applicationSettings section looks like in web.config:
<applicationSettings>
<Applicaton1.Properties.Settings>
<setting name="DefaultUserID" serializeAs="String">
<value>BatchReader</value>
</setting>
<setting name="DefaultPaymentFrequencyCode" serializeAs="String">
<value>0</value>
</setting>
<setting name="DefaultPaymentTypeCode" serializeAs="String">
<value>1</value>
</setting>
</Application1.Properties.Settings>
You should be able to read it exactly the same as any asp.net app.
Make sure you add reference to:
System.Configuration
In Web.config add your key:
<appSettings>
<add key="key" value="hello key" />
</appSettings>
Include System.Configuration in your Bootstrapper:
namespace Test
{
using System.Configuration;
using Nancy;
using Nancy.Authentication.Forms;
using Nancy.Bootstrapper;
using Nancy.TinyIoc;
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup (TinyIoCContainer container,
IPipelines pipelines)
{
base.ApplicationStartup (container, pipelines);
StaticConfiguration.DisableErrorTraces = false;
StaticConfiguration.EnableRequestTracing = true;
}
protected override void ConfigureApplicationContainer (TinyIoCContainer
container)
{
base.ConfigureApplicationContainer (container);
var key = ConfigurationManager
.AppSettings.Get ("key")
}
protected override void ConfigureRequestContainer (TinyIoCContainer container,
NancyContext context)
{
base.ConfigureRequestContainer (container, context);
}
protected override void RequestStartup (TinyIoCContainer container,
IPipelines pipelines,
NancyContext context)
{
base.RequestStartup (container, pipelines, context);
}
}
}
Thats it! :)
What conventional ways would you use in WebApi that are missing in Nancy ?
For a web app I think you should really be using System.Web.Configuration:
using System.Web.Configuration
For example:
var someVar = WebConfigurationManager.AppSettings["SomeSetting"];
See here for more information.
I created a Web Api OData controller and try to view it directly but it's not working.
WebApiConfig config
public static void Register(HttpConfiguration config)
{
config.Routes.MapODataRoute("odata", "odata", GetEdmModel());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
public static IEdmModel GetEdmModel()
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers");
builder.Namespace = "PackageManager.Models";
return builder.GetEdmModel();
}
in App start calling registering:
WebApiConfig.Register(GlobalConfiguration.Configuration);
Api controller:
public class CustomersController : EntitySetController<Customer, string>
{
NorthwindDbContext _Context = new NorthwindDbContext();
[Queryable]
public override IQueryable<Customer> Get()
{
return _Context.Customers;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
_Context.Dispose();
}
}
Try to access :
localhost:2375/odata/Customers
then always getting error:
The resource cannot be found.
Just add follow to web.config on system.webServer\handler:
<add name="ApiURIs-ISAPI-Integrated-4.0" path="*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
or replace
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
to
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
(Different - one dot in path-attribute)
#Parwej - Try writing the below code in your webapi.config instead of your code. Also make sure that proper case is followed in your URL as Odata url is case sensitive. And dont forget to use using System.Web.Http.OData instead of System.Web.Odata in controller. This should work.
public static void Register(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("Customers");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
}
I made a custom handler for test purposes, which looks like:
namespace MVCHttpHandlerProject
{
public class SomeHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
context.Response.Write("SomeHandler test");
}
}
}
Then I added to my web.config next lines:
<httpHandlers>
<add path="SomeHandler.axd" verb="*" type="MVCHttpHandlerProject.SomeHandler, MVCHttpHandlerProject" /></httpHandlers>
and in <system.webServer>
<handlers>
<add path="SomeHandler.axd" verb="*" type="MVCHttpHandlerProject.SomeHandler, MVCHttpHandlerProject" name="SomeHandler.axd"/>
</handlers>
My Global.asax.cs was not modified and looks exactly as when it was generated with routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); in RegisterRoutes method.
Still, when I try to get to "http://localhost/Home/SomeHandler.axd", "The resource cannot be found" error occurs. Why? Did I miss something? How should I fix this?
You should be requesting http://localhost/SomeHandler.axd instead of http://localhost/Home/SomeHandler.axd. There's no Home.
I am getting the following error in:
The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer.
With the following code:
[TestClass]
public class UnitTest1
{
[AssemblyInitialize]
public static void AssemblySetup(TestContext context)
{
}
[TestMethod]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("C:\\SomePath", "/")]
[UrlToTest("http://localhost/HomeView.aspx")]
public void TestMethod1()
{
using(IE ie = new IE("http://localhost/HomeView.aspx",true))
{
ie.TextField(Find.ById("MainContent_txtDLNumber")).TypeText("a235801945550");
}
}
}
Is there a different approach for using WatIn with MsTest?
You will probably need to adjust your config accordingly, below should give you a clue
<configuration>
<configSections>
<sectionGroup name="NUnit">
<section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<NUnit>
<TestRunner>
<!-- Valid values are STA,MTA. Others ignored. -->
<add key="ApartmentState" value="STA" />
</TestRunner>
</NUnit>
</configuration>
Consider updating your code to use NUnit 2.5 with RequiresSTA attribute.
Try this instead:
[STAThread]
static void Main(string[] args)
{
}