Skip to main content

Accept only int value for ASP.NET Web API action method parameter

In this short post, find solution to how to accept only int value for ASP.NET Web API action method parameter. Below is an example of Web API action method which accepts an int type parameter. And it returns the same value as response.

// GET api/values/5
[HttpGet("{id}")]
public string Get(int id)
{
    return id.ToString();
}

So when you hit http://localhost:59845/api/values/5, you get “5” returned in response. And when you hit http://localhost:59845/api/values/abc, then you get “0” as returned. But what if, you want to accept only integer value and if input parameter is not “int” then return a 404 error.

With attribute routing, you can also define the data type constraint for parameter. So updated action method is,

// GET api/values/5
[HttpGet("{id:int}")]
public string Get(int id)
{
    return id.ToString();
}

So now when you hit http://localhost:59845/api/values/abc, then you shall get 404 error. You can use any data type for route constraint as per your requirement.

Hope you like it. Stay tuned for more!.

Install ASP.NET Core RC2

A First look at ASP.NET Core 1.0 RC2

Previously I posted about what is expected in ASP.NET Core 1.0 RC2 and then posted about what’s new in ASP.NET Core 1.0 RC2. Some of the important changes from RC1 to RC2 are the introduction of new command line tool named “dotnet” to replace “dnx” and moving towards .NET Platform Standards. And I think we are now getting closer to ASP.NET Core 1.0 RC2 release as in last week ASP.NET team released a sample ASP.NET Core 1.0 RC2 project. In this post, I will take you through how to download, build and run this sample project. Read More

How ASPNET Core 10 Middleware is different from HttpModule

How ASP.NET Core 1.0 Middleware is different from HttpModule

Earlier I posted about changes in ASP.NET Core 1.0 and one of the biggest change is in HTTP pipeline. We as ASP.NET developers are quite familiar with HttpHandler and HttpModules but with this new version of ASP.NET, they are gone. They are replaced with a new better, cleaner and easy to implement approach called “Middleware“. You can think of Middleware is a combination of HttpHandler and HttpModule. In this post, find out how ASP.NET Core 1.0 Middleware is different from HttpModule.
Read More

How to rename wwwroot folder in ASP.NET Core 1.0

As mentioned earlier in my post quick summary of what’s changed in ASP.NET Core 1.0, wwwroot folder is new addition to ASP.NET Core 1.0 project structure. It’s place to keep all your static content like images, css and js files. Though, you can remove this folder from your project but if it is present then ASP.NET Core 1.0 expects folder with name wwwroot. And you can’t change the name directly from wwwroot to anything of your choice, as it works on conventions. In this post, let’s see how to rename wwwroot folder in ASP.NET Core 1.0.
Read More

What's new in ASP.NET 5 RC 2 or ASP.NET Core 1.0

What’s new in ASP.NET 5 RC 2 or ASP.NET Core 1.0 RC

ASP.NET 5 RC 2 or ASP.NET Core 1.0 RC as ASP.NET 5 is now ASP.NET Core 1.0 should be out mid Feb 2016. Since we are still not sure about the new release name, so for this post let’s continue to use ASP.NET 5 RC 2. Earlier I posted about what’s coming in ASP.NET 5 RC 2, in which I mentioned about a new dotnet cli tool to replace DNX commands. In this post, let’s take a looks at what’s new in ASP.NET 5 RC 2 release as shown by ASP.NET team in NDC conference earlier this year. This is not a complete list and I believe this list will grow when ASP.NET 5 RC 2 comes. Read More

Difference between .NET Framework and .NET Core

ASP.NET 5 is now ASP.NET Core 1.0

Last week Microsoft announced that ASP.NET 5.0 has been renamed to ASP.NET Core 1.0. And it’s a welcome change and quite needed to address the confusion. As ASP.NET 5 is completely a new platform to build cross platform applications and it was written from scrath. The latest version of ASP.NET is 4.6 and It is possible to run ASP.NET 5 on top of the full .NET 4.6 Framework/platform or to run ASP.NET 5 on .NET Core.
Read More