Thursday, October 9, 2014

Remote access to IIS Express

If you try to access the IIS Express website from a remote computer, you wont be able to do it unless you follow these steps.

Steps:

Open C:\Users\[YourName]\Documents\IISExpress\config\applicationhost.config 
Look for <sites> section
In there find your site

<site name="MyWeb" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="E:\MyWeb" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:52891:localhost" />
        <binding protocol="http" bindingInformation="*:8080:192.168.1.6" />
    </bindings>
</site>

Now, create another binding as highlighted. IP address is your computers' IP, port is whatever the port you want.

Allow incoming connections: Open command prompt as admin
netsh http add urlacl url=http://192.168.1.6:8080/ user=everyone
Open firewall:
netsh advfirewall firewall add rule name="IISExpressWeb" dir=in protocol=tcp localport=8080 profile=private remoteip=localsubnet action=allow

Tuesday, October 7, 2014

Web API 2 - Route Not Found (404)

It worked before, now it doesn't?

Here's the checklist.


  • Web API routes registration.

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        GlobalConfiguration.Configure(WebApiConfig.Register);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles); 
    }

Web API config should come before normal Routes.

  • Route Prefix
 change [Route("~/api/sendemail")] to [Route("/api/sendemail")]

Prefix "~" is not needed.

  • Route values
[Route("{myId:int}/sendemail")]
[HttpPost()]
public bool SendEmail(int id, ContactModel data)
{
    return Ok();
}
notice the route defines a myId but method accepts an id. This will give a 404
Change the id or myId like this for it to work.

[Route("{myId:int}/sendemail")]
[HttpPost()]
public bool SendEmail(int myId, ContactModel data){
    return Ok();
}