asp.net mvc 4 - How to encrypt the query string ID in mvc4 ActionLink -
hi how can pass encrypted id in actionlink, written in view
@model ienumerable<forumapp.tbltechnology> @foreach (var item in model) { string techname=item.techname; @html.actionlink(techname, "details","home", new { topicid = item.techid },null) // here encrypt topicid <br /> <br /> @html.displayfor(modelitem => item.techdesc) }
here couple of simple methods can utilize encode/decode. encoded value not secure, , can see, decoding trivial. if goal obfuscate id, work. if need secure it, should take different approach.
public string encode( string encodeme ) { byte[] encoded = system.text.encoding.utf8.getbytes( encodeme ); homecoming convert.tobase64string( encoded ); } public static string decode( string decodeme ) { byte[] encoded = convert.frombase64string( decodeme ); homecoming system.text.encoding.utf8.getstring( encoded ); } so place these methods in controller, , pass encoded techid view viewbag
int techid = 1; var encoded = encode(id.tostring()); viewbag.encoded = encoded; and utilize in link
@html.actionlink(techname, "details","home", new { topicid = viewbag.encoded },null) (though, should consider using view model. viewbag, while convienent , easy way pass info view, not considered best practice. becoming comfortable view models , typed views create mvc life much easier in future. not mention, produce cleaner , more maintainable code follow you.)
asp.net-mvc-4
No comments:
Post a Comment