Single controller with multiple GET methods in ASP.NET Web API

[Origin]: https://stackoverflow.com/questions/9499794/single-controller-with-multiple-get-methods-in-asp-net-web-api

In Web API I had a class of similar structure:

public class SomeController : ApiController
{
    [WebGet(UriTemplate = "{itemSource}/Items")]
    public SomeValue GetItems(CustomParam parameter) { ... }

    [WebGet(UriTemplate = "{itemSource}/Items/{parent}")]
    public SomeValue GetChildItems(CustomParam parameter, SomeObject parent) { ... }
}

Since we could map individual methods, it was very simple to get the right request at the right place. For similar class which had only a single GET method but also had an Object parameter, I successfully used IActionValueBinder. However, in the case described above I get the following error:

Multiple actions were found that match the request: 

SomeValue GetItems(CustomParam parameter) on type SomeType

SomeValue GetChildItems(CustomParam parameter, SomeObject parent) on type SomeType

I am trying to approach this problem by overriding the ExecuteAsync method of ApiController but with no luck so far. Any advice on this issue?

Edit: I forgot to mention that now I am trying to move this code on ASP.NET Web API which has a different approach to routing. The question is, how do I make the code work on ASP.NET Web API?

shareedit

This is the best way I have found to support extra GET methods and support the normal REST methods as well. Add the following routes to your WebApiConfig:

routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });
routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");
routes.MapHttpRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new {action = "Post"}, new {httpMethod = new HttpMethodConstraint(HttpMethod.Post)});

I verified this solution with the test class below. I was able to successfully hit each method in my controller below:

public class TestController : ApiController
{
    public string Get()
    {
        return string.Empty;
    }

    public string Get(int id)
    {
        return string.Empty;
    }

    public string GetAll()
    {
        return string.Empty;
    }

    public void Post([FromBody]string value)
    {
    }

    public void Put(int id, [FromBody]string value)
    {
    }

    public void Delete(int id)
    {
    }
}

I verified that it supports the following requests:

GET /Test
GET /Test/1
GET /Test/GetAll
POST /Test
PUT /Test/1
DELETE /Test/1

Note That if your extra GET actions do not begin with ‘Get’ you may want to add an HttpGet attribute to the method.

shareedit
Advertisements

Error converting data type varchar to varbinary in sql only for few values

[Origin]: https://stackoverflow.com/questions/45319910/error-converting-data-type-varchar-to-varbinary-in-sql-only-for-few-values

IF(ISNUMERIC(RTRIM(LTRIM('83B1B88'))) = 0)
select CONVERT(VARCHAR, CONVERT(INT, CONVERT(VARBINARY, CONVERT(VARCHAR, '0x' + '83B1B88'), 1)))

I am not sure why sql is throwing Error converting data type varchar to varbinary I am getting this exception only for values like 1B91B32,169DF9013077B5

Can anyone help me out ?

shareedit

You’re passing a style of 1 to CONVERT.

If the data_type is a binary type, the expression must be a character expression. The expression must be composed of an even number of hexadecimal digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, a, b, c, d, e, f). If the style is set to 1 the characters 0x must be the first two characters in the expression. If the expression contains an odd number of characters or if any of the characters are invalid an error is raised.

My emphasis.

All of your examples of invalid sequences appear to have odd lengths. You should add a padding 0 if you have an odd length sequence. Whether you pad at the start or the end depends on your exact requirements, where the sequence came from, etc.

shareedit

SQL Server – Error 0xc0202049: Data Flow Task 1: Failure inserting into the read-only column

[Origin]: https://stackoverflow.com/questions/20948819/error-0xc0202049-data-flow-task-1-failure-inserting-into-the-read-only-column/20953512

I am using SQL Server 2008 import and export wizard. I need to import a database. I opened the SQL server import/export wizard and I went through the following actions:-

  1. for the destination I chose “SQL server native client 10”.
  2. then I selected copy data from one or more tables or view.
  3. SSIS run immediately

but i got the following errors,

Operation stopped…

  • Initializing Data Flow Task (Success)
  • Initializing Connections (Success)
  • Setting SQL Command (Success)
  • Setting Source Connection (Success)
  • Setting Destination Connection (Success)
  • Validating (Error)

Messages Error 0xc0202049: Data Flow Task 1: Failure inserting into the read-only column “ActionID”. (SQL Server Import and Export Wizard)

Error 0xc0202045: Data Flow Task 1: Column metadata validation failed. (SQL Server Import and Export Wizard)

Error 0xc004706b: Data Flow Task 1: “component “Destination – AuditActions” (22)” failed validation and returned validation status “VS_ISBROKEN”. (SQL Server Import and Export Wizard)

It seems that I can not import identity columns and timestamps columns, so how I can force these values to be imported?

shareedit

 

Before Importing date execute the below query to set identity insert on

SET IDENTITY_INSERT TableName ON

// Do the Import Operations

after Importing date execute the below query to set identity insert off

SET IDENTITY_INSERT TableName OFF
shareedit

 

With all due respect to Kishore’s answer, that approach is valid if you’re modifying the packages. Since you’re using the import/export wizard, the more straight forward approach would be to check the Enable Identity Insert box on the Column Mappings tab.

In the Import Export Wizard, after selecting the table for copy, click the Edit Mappings... button

enter image description here

In the resulting screen, click the Enable identity insert property and your identities will be replicated over.

enter image description here

Same net result as issuing SET IDENTITY_INSERT TableName ON & OFF but this is done automagically at build time.

shareedit

How to remove error about glyphicons-halflings-regular.woff2 not found

[Origin]: https://stackoverflow.com/questions/32300578/how-to-remove-error-about-glyphicons-halflings-regular-woff2-not-found

This problem happens because IIS does not know about woff and woff2 file mime types.

Solution 1:

Add this lines in your web.config project:

 <system.webServer>
  ...
  </modules>
    <staticContent>
      <remove fileExtension=".woff" />
      <mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
      <remove fileExtension=".woff2" />
      <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
    </staticContent>

Solution 2:

On IIS project page:

Step 1: Go to your project IIS home page and double click on MIME Types button:

Step 1

Step 2: Click on Add button from Actions menu: Step 2

Step 3: In the middle of the screen appears a window and in this window you need to add the two lines from solution 1: Step 3

shareedit

Stop Visual Studio from launching a new browser window when starting debug?

[Origin]: https://stackoverflow.com/questions/716494/stop-visual-studio-from-launching-a-new-browser-window-when-starting-debug

I already have a window open with the web site I’m debugging. I don’t need VS to launch another one for me every time I need to debug.

Is there a way to stop this behavior?

shareedit

Open your startup project’s properties (Project->{ProjectName} Properties… from the main menu or right click your project in the Solution Explorer and choose Properties), then navigate to the Webtab and under Start Action choose Don’t open a page. Wait for a request from an external application.

You will still be able to use any browser (or Fiddler, whatever) to access the running application, but it won’t open the browser window automatically, it’ll just start in the background and wait for any requests.

shareedit

Running two projects at once in Visual Studio

[Origin]: https://stackoverflow.com/questions/3850019/running-two-projects-at-once-in-visual-studio

I created a solution in Visual C# 2010 Express that contains two projects: one is the client, the other is the server. I would like to debug both at the same time, but I can only seem to run one of the projects during debugging.

Is there a way to run both at once?

shareedit

Go to Solution properties -> Common properties -> Startup Project and select Multiple startup projects.

solution properties dialog

shareedit

Max has the best solution for when you always want to start both projects, but you can also right click a project and choose Debug > Start New Instance.

This is an option when you only occasionally need to start the second project or when you need to delay the start of the second project (maybe the server needs to get up and running before the client tries to connect, or something).

shareedit

Fix Office Outlook 2013 slow and laggy

[Origin]: https://www.opsactive.com/fix-office-outlook-2013-slow-and-laggy/

 MICHAEL HOW TO GUIDES OFFICE 365

After upgrading to Outlook 2013 I noticed that the performance was incredibly slow when switching between Mail, Contacts and Calendar,  there would often be a several second delay before the screen changed. When searching for Contacts it could take 15 seconds to return the results. Now I’m not one to complain (too much) but if my local PC a quad core beast was taking 15 seconds to return results from a couple of hundred contacts, how can Google return 53,000,000 results when searching for the same contact? Anyway after a couple of months worth of persevering I decided to look for a solution which turns out to be graphics related.

UPDATE – 28/05/2013!!!

The original fix required playing in the registry. If you manage multiple systems via AD then you can still use that on a global scale but if its only 1 or 2 computers then this option may be quicker! Thanks to Kurt Conway for pointing this out in the comments. There may be a group policy available for this but demand has not been that great, I will see if I can load the new policies up and check but in the meantime you can try this.

Open Outlook, Click File (top left), Then Options. Now click on advanced and scroll down to where you see “Disable hardware graphic acceleration” – Simply place a tick in the box, restart Outlook and it should be a lot faster.

Disable Hardware Acceleration

Or if you like doing things the long way –

ORIGINAL FIX

This is the fix for my machine which is Windows 8, I’m sure Windows 7 will be similar if not identical.

Step 1 – open your registry. Press the windows key and type “Regedit” then press enter or click the Regedit icon, windows 7 users you can type Regedit in your start bar.

Regedit win 7 8

 

Step 2 – Browse to: COMPUTER \ HKEY_CURRENT_USER \ Software \ Microsoft \ Office \ 15.0 \ Common\ Graphics. If you don not have a “Graphics” right click on “Common” and choose “New” then “Key” and create “Graphics” (no quotes)

Step 3 – Once on the Graphics “folder” right click it and choose new DWORD (32-bit) Value

Create DWORD

Step 4 – Give the new DWORD a Value name: DisableHardwareAcceleration and give it a Value of 1 – Click OK to save it.

Disable Hardware Acceleration

 

Step 5 – Open Office and you should see a massive improvement in the speed of switching between contacts, calendar and you inbox.

Below is a video guiding you through this process, Hope it helps.