ASP.NET MVC/C# development tips record.

ASP.NET MVC/C# development tips record.

1. When writing CSS, first replace the original cshtml <style>...</style>, because this will provide relevant smart prompts. After writing, make it into a separate css file and use @Styles.Render or directly link to load it. (The current version of VS has improved this)

2. Using View(...) in Action will pass the contents of Viewbag, but using RedirectToAction(...) may not pass the contents of Viewbag.

If you use reflection to process enum types, the code is as follows:

view plaincopy View the code snippet on CODE derived from my code snippet

  1. Type XXXXX= assembly.GetType( "YYYYY" , true );
  2. Array enumArray = Enum.GetValues(XXXXX);

3. If you use database first, and then add the identity keyword to identify the field during the project, remember to double-click the edmx file, right-click, and update from database. Otherwise, the IDENTITY_INSERT problem will be reported.

4Use CTRL+SHIFT+F to call out a more powerful search window that will list all the results found instead of selecting them one by one.

This VsVim plugin is awesome!

5 MVC5 binds elements by name, not id. But generally, for safety reasons, id and name are set to be the same.

6 view plaincopy View the code snippet derived from my code snippet on CODE

7 In an Action called Create, return Index(). If the last sentence of Index() is return View(), it will still jump to create.cshtml, indicating that the top-level Action determines the default cshtml page.

8 Damn, I spent a lot of time today trying to deploy ASP.NET MVC5 to Alibaba Cloud. I struggled with IIS for a long time. When I visited the website, it kept saying that MvcApplication could not be loaded. It turned out that I had to change Debug to Release. I may also need to pay attention to the CPU platform issue.

9 In VS2013, if you find that the best suggestion item is not selected by default in the code hint, but is only displayed (that is, it cannot be completed by the Enter key), press ctrl+alt+space to activate the suggestion model.

10 Microsoft.VisualStudio.TestTools.UnitTesting is used in engineering testing. The reference that needs to be added is Microsoft.VisualStudio.QualityTools.UnitTestFramework

11 For some problems, due to incomplete or difficult to understand documentation, you may not know the name of some types. In this case, you can get the element in the code and then use GetType().ToString() to understand it. For example, when working with Open XML.

12 In C#, the dictionary is different from that in C++. If a key does not exist but is referenced by ["xxx"], an exception will be thrown. The same is true when calling the Add function. If the key already exists, an exception will also be thrown. Therefore, it is best to use containskey to confirm whether it exists.

13 LINQ is ultimately converted into a SQL query, so please note that even though it looks like a simple comparison of strings "==", it is actually case-insensitive, just like SQL.

14 When using ASP.NET MVC to return json, remember to use the second parameter of return Json(..) to allowGet, otherwise the server will return 500.

15 If the JS code is made into a separate file for reference, then the URL in the js, such as var actionUri = 'Register', defaults to the method under the controller corresponding to the CSHTML file where the js is located.

16 If you have money, you must buy a set of Resharper VS plug-in! It is really cool! Blast! God!

17 If you use a reference type variable and want to add it to a list or other place of use, try to use clone, otherwise it will point to the same object.

18 Unless the added thing is Microsoft's, such as Entity Framework, other libraries should never be managed, downloaded, updated, etc. with Nuget, otherwise the version is often too far behind and the bugs are annoying, such as raphael.js.

19 I found something very strange. If some cs files used in ASP.NET MVC5 are not from this project, but are referenced from other projects, then even so, if you directly copy this project to the website directory of IIS (without copying the referenced files), it can run normally. I guess these codes have actually been compiled into the Bin file.

20 Class B inherits class A. If B can modify the return value of the inherited function, this is also considered as overriding, that is, the function needs to be new. If it is a data member, new must be used if the name is the same. However, if it is a virtual data member, the type must be the same to override.

<<:  What are the differences among mvc1, mvc2 and mvc3?

>>:  Apple releases second iOS 9 beta to developers

Recommend

Creating a new lean model: Six most systematic aspects of lean operations

Creating a new lean model: Introduction to the si...

Why does eating nuts make your saliva taste like water?

Nuts are nutritious and delicious. The reason for...

Weighing more than two pigs! How was this champion giant pumpkin grown?

Your browser does not support the video tag Autho...

Building a giant "super telescope" on the moon? It's not easy

Recently, French scientists announced a plan to b...

How to formulate a promotion plan?

Many times, the process of formulating a promotio...

Sit on it and slide down? Snowmobile is not as simple as you think

The Winter Olympics is about to open. When talkin...

Why are your conversions always so low?

As bidders, we may also be familiar with the term...

How to promote online for free (low cost) (Part 1)

Recently, many people have asked me a question, h...