How to display the ID in the action to the query string.

Source: Internet
Author: User
Tags actionlink

Recently encountered a disgusting problem:

Actions in testcontroller are as follows:

 
  1 PublicActionresult index (Question)
2 {
3 ReturnView (question );
4 }

 

The question parameter is defined as follows:

 

  1     Public     Class  Question
2 {
3 Public String Id { Get ; Set ;}
4 Public String Name { Get ; Set ;}
5 }

 

Then I use HTML. the href of the a tag generated by actionlink ("example", "Index", "test", new question {id = "12312312312", name = "asdfasdfasdf"}, null) is:

Http: //.../test/home/12312312312? Name = asdfasdfasdf

That is to say, it places the ID in the route by default (basically because we have the default route: {controller}/{action}/{ID }, the reason for the duplicate names of the IDS ). this book is nothing, because the server action can also be normally bound to parameters. however, what is abnormal is that my ID often contains "/" such

  Html. actionlink (  "  Example  "  ,  "  Index  "  ,  "  Test "  ,  New  Question {ID  =  "  123/123/123/12  "  , Name  =  "  Asdfasdfasdf  "  },  Null  ) 

This lineCodeThe generated result is: http: //.../test/home/123/123/12312? Name = asdfasdfasdf is definitely an invalid URL. and even if I use the URL. after encode the value of ID, it seems to be correct: http: // localhost: 3427/test/index/123% 252f123% 252f12312? Name = asdfg but % 252f will change to/after arriving at the server and then match the action controller ID, so our ID value is still 123/123/12312. this issue is indeed quite tangled.

Solution:

(1): Since the default route contains the ID Section and the ID is placed in the route, we can start with the default route, we can change the ID in the default route to another one, for example, myid: {controller}/{action}/{myid}, or simply delete the {ID} segment. in fact, you can also force 123/123/12312 to match the ID in the form of {* ID}. However, this form is too broad and may easily affect other codes, I chose to delete the {ID} section directly.

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.