How to define custom url while hostingdotnet core 2.0 application in Kestrel - kestrel-http-server

I am hosting my angular application using DotNet Core 2.0 and here is my hosting configuration in program.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Net;
namespace Samples.Web
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(options=>{
options.Listen(IPAddress.Loopback,5050);
})
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Build();
}
}
I overrided default port number 5000 of Kestrel using options.So http://localhost:5050 is working but how can I make a custom host url something like http://localhost:5050/samples working as well?
I tried UseUrls which didn't work. Thanks for help.

Related

Injecting Session with StructureMap in ASP .Net MVC

I am trying to inject ASP .Net MVC Session into a Controller by providing in interface for it using StructureMap. But StructureMap complaints while trying to do this as HttpContext.Current is not initialized at the time of registry initialization. I am sure there is a way around it I have yet to find. Please point me into the right direction.
Here is my code for more information:
DefaultRegistry.cs:
using System.Web;
using Company.O365Web.Infrastructure;
using StructureMap.Configuration.DSL;
using StructureMap.Graph;
namespace Company.O365Web.DependencyResolution
{
public class DefaultRegistry : Registry
{
#region Constructors and Destructors
public DefaultRegistry()
{
Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.With(new ControllerConvention());
});
For<ISessionState>()
.Singleton()
.Use(new SessionStateProvider(new HttpSessionStateWrapper(HttpContext.Current.Session)));
}
#endregion
}
}

'System.Web.HttpContext' does not contain a definition for 'GetOwinContext'

I have the following error :'System.Web.HttpContext' does not contain a definition for 'GetOwinContext'
whenever I debug my project. this error is in the web API page, although it doesn't show an error in the default AccounController.cs and the nuget package is installed.what is missing?
using IdentitySample.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using System.Web;
using System.Threading.Tasks;
using task.Models;
using System.Globalization;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
ok I found the solution since I already installed the nuget package all I have to do is the following:
return _userManager ?? HttpContext.**current**.GetOwinContext().GetUserManager<ApplicationUserManager>();

After install owin package to my vs2012 i cant found my create owin startup class

I have installed Microsoft Owin package to my VS2012 but while creating a new class i could not see my owin start up class for creating a new class.
It should be created by the SignalR package I think?
[assembly: OwinStartup(typeof(SignalRConfig))]
namespace MyApp.App_Start
{
public static class SignalRConfig
{
public static void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}

WebAPI ActionName not routing after publish: 404 - File or directory not found

When testing my action in VS, everything is working fine.
After deploying, /site/controller responds as expected (403) while /site/controller/action responds with a 404.
Other api projects deployed from the same solution to the same root are working as expected.
Each project is configured as an application in IIS.
Can you give me a clue on how to proceed or what information to add here that would clarify the configuration?
Global.asax:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
namespace ssa
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(
new QueryStringMapping("type", "json", new MediaTypeHeaderValue("application/json")));
GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(
new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));
}
}
}
WebApiConfig.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace ssa
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
Iam not sure if you need to register all routes in your global.asax, but check this:
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs
Check is your server configured to run targeted .net version it may be some differences between web api 1.0 and 2.0 at the server point of view.

Signalr in Asp.Net MVC 4 app

I'm working on Asp.net MVC app, I'd like to use signalr.
Signalr to be hosted in IIS.
I added the signalr dlls and added the reference to it.
In Global.asax,
I put the code,
inside Application_Start()
" RouteTable.Routes.MapHubs();"
I've pasted the Global.asax,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
RouteTable.Routes.MapHubs();
}
}
When i run the app, say, "http://localhost:4432",
When I try "http://localhost:4432/signalr/hubs", I get exception as follows,
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /signalr/hubs
Signalr version : I tried with V1.1.25 and 2.0
Someone direct me what I should do to get it corrected?
1.write a hub extends Hub
public class MessageHub : Hub
2.check web.config
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
I tried,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteTable.Routes.MapHubs();
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
Put "RouteTable.Routes.MapHubs();" in the first line of Application_Start() method.

Resources