How to rewrite some default WebApi rules

Source: Internet
Author: User

How to rewrite some default WebApi rules
Why?

Recently, the company has been promoting the SOA framework. For the first time, I have been very familiar with this technology (but I still ignore it). I feel pretty good. I just want to make some effort to implement a simple SOA framework.

I used mvc for development. In my mind, WebApi and Mvc seem to be the same. I started playing WebApi with such a preset and was abused so that I could not find it.

The reason for abuse is that Mvc and WebApi are slightly different in details, for example:

This is the biggest difference I know between the two technologies. Other technologies are not found or noticed, and these differences may also be caused by the fact that they are not used. After all, it takes a long time to access webapis. If I have made some mistakes, please correct them.

In view of these two differences, I have checked a lot of information and there is no way to solve them. The first one is okay, just add a feature, in the second case, it seems that even if [FromBody] is added, it still does not work. It feels like a bunch of restrictions. Then, since there are so many unpleasant things for me, let me modify it.

The transformation objectives include:

After setting the goal, why does Microsoft Design WebApi like this? Maybe it makes sense.

The goal was fixed, and it was really big. At first I wanted to refer to the implementation of the company's SOA framework, but because I used the OWIN Technology for hosting, however, after reading the company's framework, it seems that it is not used. In short, it means that after reading the framework for a long time, I didn't understand where to start. Instead, the more I see it, the more I see it. After all, it is not exactly the same technology, so do it yourself.

Okay, so much nonsense. Let's get started. First come to a link, without this article I will not be able to transform successful: http://www.cnblogs.com/beginor/archive/2012/03/22/2411496.html

OWIN host

In fact, there are a lot of online websites. I mainly post code. Otherwise, the following sections cannot be written.

Some less important code is omitted. Services. Add and Replace can be understood literally, but I have not tried whether to write as above.

Limits on Controllers

This is actually relatively simple. In the test, the WebApi does not seem to call the GetControllerMapping method and directly calls the SelectController method. There are two Get methods called in the last method, get only encapsulates the TryGetValue function for Obtaining values from the dictionary. The InitControllers method is to find the class that inherits the ApiController from all the current programs and cache it after finding it. This code is relatively simple as a whole.

Action restrictions

The GetActionMapping method is simple. Find all Action methods from the Controller type and return

The SelectAction method is relatively complex. In fact, it is the logic of the second target. It seems that there are a lot of code and it is very difficult.

Restrictions on Action Parameters

This is difficult. I tried it for a long time before it was successful, and there is still a pitfall.

This class is actually used. the default Implementation of Net is changed a little, that is, from 17th rows to 20th rows. The current judgment logic is obtained from the Url or Form if the parameter type is the basic type, this logic is written in MyValueProviderFactory, and ValueProviderAttribute is. net. Others have not been changed, even if they do not change, because they do not understand the meaning of the code at a time.

This is very simple, skipped.

The core is the GetValue method. First, the value in the query string is determined. If no value exists, the system determines whether there is a cache. If no value exists, the system checks whether the form parameter is in the Body again. If yes, the system reads the value directly. This is actually not intended to use the cache at the beginning, but if it is not used, there will be a problem. If an Action has multiple parameters, it will be suspended.

The reason is:

The subsequent logic is actually simple.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.