Linux. NET hands-on change of loach (below)

Source: Internet
Author: User

In the previous round, we changed the Access database that is only available for running in Windows to support cross-platform MYSQL databases without any nutrition. In this round, we will deploy and debug the modified project in Linux to adapt it to Linux.. NET runtime environment.

In this round, we will discuss the study:

1. One lie leads to another.

2. What should I do if I encounter a lot of case-insensitive problems?

3. requestValidationMode?

4. Different rooms on the same roof

1. One lie leads to another.

After we deploy small Loach on Linux, the home page is usually normal (the home page can be opened and can be read), but when we click back-end management, the page is getting strange. It does not show a user name and password interface as we imagined, but a "problem" Page shown in:

By reading the stack trace, we probably know that the program uses an "AdminPage. after CheckLoginAndPermission is entered, an error is reported. Therefore, we need to first determine whether the thing called "CheckLoginAndPermission" is in the Mono or other third-party class libraries or in our code.

The judgment method is quite simple. Press Ctrl + Shirt + F on Visual Studio. That's right, it's the search function. As long as we can find the relevant code in the project, then it turns out that the change method is something in our project.

Through the search, the results really let us find the crux of the problem, so we just went into the method, the code of this method is as follows:

(! + = (User =) + (StringHelper. GetMD5 (user. UserId + HttpContext. Current. Server. UrlEncode (user. UserName) + user. Password )! = + (PageUtils. currentUser. status =, [] plist = [] {, (PageUtils. currentUser. type = (pageName = (p (pageName =,CheckLoginAndPermission

At first glance, a method for verifying account logon and permissions automatically jumps to their respective pages if not met. There is nothing special and there is no problem. However, the program exception occurs here, so we need to find it.

Readers may immediately think of pressing "F5" or "attaching to a process" and relying on Visual Studio, a powerful IDE, to locate the problem. However, do not forget that our program is normal in Windows and the current operating system is not Windows. Therefore, we cannot use the functions of Visual Studio. Maybe some readers still know that the "Mono Develop" IDE can be used in Linux. Unfortunately, this tool is not installed in Linux, even Xwindows is not installed, the Linux operating level is only "init-3" level, to get "Mono Develop" too much trouble, we need some interesting means to locate our problem.

Let's look back at it. Since the project is successfully released, it proves that the project is successfully compiled, but an error is reported during runtime, which indicates that this is a runtime exception. Runtime exceptions are also common. For example, when a program reads a non-existent file or database connection string is written incorrectly, these are all runtime exceptions, the program stops running and prompts an error only when the program is running at this step.

Based on this principle, we can define some "lie" (manually add some runtime errors), let the program run here to terminate and prompt errors, and compare the errors prompted by the program, we can find out which line of code is wrong in the project, and use a lie to lead to another lie.

For example, I add a "lie" in checking whether to log in ".

(! + = A =. Parse (user =) + (StringHelper. GetMD5 (user. UserId + HttpContext. Current. Server. UrlEncode (user. UserName) + user. Password )! = + (PageUtils. currentUser. status =, [] plist = [] {, (PageUtils. currentUser. type = (pageName = (p (pageName =,Lie CheckLoginAndPermission

Refresh the page after compilation and release

We get this runtime exception. The figure is obviously different from the previous one. It proves that the exception just now is under the "lie.

We constantly move our "lie" (manually added runtime errors) down until it raises the true "lie" (original runtime errors.

The iteration through this method is probably located here:

Combined with the error "Object reference not set to an instance of an object" reported to us, we can perform a promotion. Here, something is null. Here, only user is the class to be instantiated (PageUtils. CurrentKey is a static attribute). We can guess that user is null.

For verification, we can do the following:

Through verification, we found that our reasoning is correct, that is, if the user is null, this failure occurs.

But why is the user empty? Or is there no checknull judgment on the user in the program of small Loach? Let's first review the user's source. The user comes from this method:

Pass in a CurrentUserId to obtain the user class. In the GetUser method, the Code is as follows:

UserInfo GetUser (UserInfo user (user. UserId =GetUser

CurrentId is actually a userid. You can obtain the user instance by comparing the values of two userids. Still wondering why not get null? Here is a trap. We have no login at all, so there is no CurrentUserId (or the userid value is null) at all. Therefore, the output of the GetUser method should also be null.

Isn't the small Loach unable to handle the case where the output is null? The answer is no. I already have a judgment in the small Loach. Otherwise, an error is reported in Windows. If the user does not log on (there will be no CurrentUserId if there is no login), the page will jump to "login. aspx page (login page ).

Logically, this is true, but not true. The page does not jump, or, more specifically, the program does not Redirect after it reaches the Redirect method and terminates the subsequent code in the "CurrentUserId" method. After looking at the "culprit" behind the scenes, it becomes easy to deal with it. We just need to manually let it terminate the subsequent code in the running method.

Add Return under Redirect (all Redirect in the method is added ):

Then compile and release again, and we can see our login page.

 

 

2. What should I do if I encounter a large number of cases?

Go to the background management page and try to add an article:

To add an image, click "insert image/file ":

Then ......, As mentioned above, "'usercontrols/upfilemanager. ascx" does not exist.

The typical case sensitivity issue is the case sensitivity of the "UserControls" folder. The solution is very simple. Change the folder name to the upper and lower case.

As I mentioned in the previous round, the Case sensitivity of small loach is still strict, basically all of them are case sensitive. However, if we are not dealing with small Loach, it is a very troublesome project, there are a lot of case-sensitivity issues in it, so we can perform a blanket-style search and modify it to make it very feasible (it is better to overwrite it ).

In the face of this situation, we also have the "authorization code". We can modify the jws script file to make Mono case insensitive to the file directory (note that it is a file directory, SQL statements are differentiated because parsing SQL statements is a database task, not a Mono task ).

You only need to open the "jws" file and set the IOMAP of Mono to all (delete the "#" in jws ).

Restart Jexus and try again:

Okay, we have fixed another problem.

 

3. requestValidationMode?

I don't remember when it was about. NET FrameWork 4.0. When we used the Rich Text Editor, there would be a "Form dangerous..." When we submitted it ......" And so on ,. NET also automatically helps us verify the messages sent back from the page, sensitive strings such as angle brackets will be automatically. NET refuse to receive, the solution is also very easy, there are a lot of online, basically is to change the Verification Mode from "4.0" (or higher) to "2.0" and then OK.

However, here, the small loach is based on. NET 2.0, so we should not see the above situation ?! Let's first try to add something:

Then click submit:

After a moment of advertisement, we can only say "~~" Now, Mono runs small loach with ASP. NET 4.0. Since it runs in. NET FrameWork 4.0 mode, we only need to use the corresponding solution.

I added "requestValidationMode" to the "httpruntime" node in "web. config" (here, I directly added it with VI without re-Compiling and publishing ):

Then we add and save the article again, and we can find our article on the homepage:

 

4. Different rooms on the same roof

After completing the configuration, the small loach is in Linux. after running exceptions in. NET, we also need to add support for the Sqlite database. Although we often hear about this database, we have never really touched on it, it was the first time (first experience?!) until the extension was added ?!), Thanks to SQL statements with high universality and ADO. NET, allowing me to extend Sqlite just by knowing that it has five data types and where to get the driver from Nuget (which is a tragedy ).

After passing the test in Windows, we can't wait to publish it to Linux, and then:

 

In Linux. NET, if the DLL is clearly in the bin directory, but the program reports that it cannot be found or cannot be loaded, it is generally either the DLL cannot be found (file case sensitivity problem) and other dependent DLL files are not loaded successfully or directly incompatible. After the case sensitivity problem is ruled out, we can find out if Mono has a dll that is driven by Sqlite.

It can be found that Mono has built-in Sqlite driver and is in two different namespaces than in MS. NET. In addition, there is an image to be viewed by readers:

Not only are the namespaces different, but the case sensitivity is also slightly different. Therefore, we won't talk about it for a thousand words. Change it ~~!

At this point, the transformation of small Loach has basically been completed, and readers who need code can find it in GitHub (the address has been mentioned in the previous article). The capability is limited. If there is something wrong with the writing, you are welcome to leave a message, if you have any suggestions or comments, please leave a message. Let's go back and see ~~!

PS: This year is the year of "code". Here, I hope you will have a happy new year and a fortune.

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.