How to move to new line in mvc5 on server side - asp.net-mvc-5.1

I use the many tag like /n and but all are not work.please suggest me the best approch for it
string shamus = "access" + "<br><br>"+"good";
string shamus = "access" + "<br/>"+"good";
string shamus = "access" + "\n"+"good";

Related

403 Invalid token error on GET user info

UPDATE: I thought I had to pass the parameters as a JSON string in the request body, but actually I need to put them on the URL (the endpoint string), so it's working now.
I'm new to Valence. I have some Salesforce Apex code (written by someone else) that creates a D2L user. The code is working fine.
I want to add an Apex method to retrieve info for an existing D2L user using the userName parameter. I've copied the existing method, changed to a GET, set the query parameter to userName, and kept everything else the same.
When I call my method, I get a 403 Invalid Token error.
Do I need to use different authorization parameters for a GET? For example, do I still need to include a timestamp?
Here's a portion of the Salesforce Apex code:
public static final String USERS = '/d2l/api/lp/1.0/users/';
String TIMESTAMP_PARAM_VALUE = String.valueOf(Datetime.now().getTime()).substring(0,10);
String method = GETMETHOD;
String action = USERS;
String signData = method + '&' + action + '&' + TIMESTAMP_PARAM_VALUE;
String userSignature = sign(signData,USER_KEY);
String appSignature = sign(signData,APP_KEY);
String SIGNED_USER_PARAM_VALUE = userSignature;
String SIGNED_APP_PARAM_VALUE = appSignature;
String endPoint = DOMAIN + action + '?' +
APP_ID_PARAM + '=' + APP_ID + '&' +
USER_ID_PARAM + '=' + USER_ID + '&' +
SIGNED_USER_PARAM + '=' + SIGNED_USER_PARAM_VALUE + '&' +
SIGNED_APP_PARAM + '=' + SIGNED_APP_PARAM_VALUE + '&' +
TIMESTAMP_PARAM + '=' + TIMESTAMP_PARAM_VALUE;
HttpRequest req = new HttpRequest();
req.setMethod(method);
req.setTimeout(30000);
req.setEndpoint(endPoint);
req.setBody('{ "orgDefinedId"' + ':' + '"' + person.Id + '" }');
I thought I had to pass the parameters as a JSON string in the request body, but actually I need to put them on the URL (the endpoint string), so it's working now

Grails How to make address show in two line?

I've a student form which there's location inside the form, when I run the app and show the form it'll look like this
Location : Jl Excel Road Ring No.36 SINGAPORE, 10110
But I want to make the location in two line like this
Location : Jl Excel Road Ring No.36
SINGAPORE, 10110
here's the gsp
<td><g:message code="location.label"/></td>
<td>${studentInstance.location}</td>
and this is the service in def show
def loc = Location.findByTidAndDeleteFlag(params.tid, "N")
if(loc != null){
studentInstance.location = loc.address1 + " " + loc.city + ", " + loc.zipCode
}
else{
studentInstance.location = ""
}
Use the br tag
studentInstance.location = loc.address1 + "<br/> " + loc.city + ", " + loc.zipCode
Then you can render directly the HTML unescaped like this:
<%=studentInstance.location%>
The default codec is probably HTML in your configuration.
Check the value of grails.views.default.codec
For more information read this:
http://grails.org/doc/2.2.1/ref/Plug-ins/codecs.html
I believe that starting from Grails 2.3.x the default views codec is HTML with XML escaping in order to prevent XSS attacks.
This is a bad approach but you can try
studentInstance.location = loc.address1 + "<br> " + loc.city + ", " + loc.zipCode
Generally, I would have each of the element of address available in view so that the styling is flexible in view than in controller, something raw would look like:
<td><g:message code="location.label"/></td>
<td>${model.address1} <br> ${model.city}, ${model.zipCode}</td>

Convert part of string to URL when displayed

I browsed around for a solution and I am sure it's a simple question but still not sure how to do that. So, I have a string that contains many words and some times it has links in it. For example:
I like the website http://somesitehere.com/somepage.html and I suggest you try it too.
I want to display the string in my view and have all links automatically converted to URLs.
#Model.MyText
Even StackOverflow gets it.
#Hunter is right.
In addition i found complete implementation in C#: http://weblogs.asp.net/farazshahkhan/archive/2008/08/09/regex-to-find-url-within-text-and-make-them-as-link.aspx.
In case original link goes down
VB.Net implementation
Protected Function MakeLink(ByVal txt As String) As String
Dim regx As New Regex("http://([\w+?\.\w+])+([a-zA-Z0-9\~\!\#\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?", RegexOptions.IgnoreCase)
Dim mactches As MatchCollection = regx.Matches(txt)
For Each match As Match In mactches
txt = txt.Replace(match.Value, "<a href='" & match.Value & "'>" & match.Value & "</a>")
Next
Return txt
End Function
C#.Net implementation
protected string MakeLink(string txt)
{
Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection mactches = regx.Matches(txt);
foreach (Match match in mactches) {
txt = txt.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
}
return txt;
}
One way to do that would be to do a Regular Expression match on a chunk of text and replace that url string with an anchor tag.
Another regex that can be used with KvanTTT answer, and has the added benefit of accepting https urls
https?://([\w+?.\w+])+([a-zA-Z0-9\~!\##\$\%\^\&*()_-\=+\/\?.:\;\'\,]*)?
.net string representation:
"https?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?"

Use string format in razor view to concat javascript variables

How can I write
var releaseName = $('#SelectedReleaseId option:selected').text()
var templateName = $('#SelectedTemplateId option:selected').text()
this:
$("#TestplanName").text(releaseName + '-' + templateName + '-' + '#Model.UserId' + '-' + '#Model.CreatedAt');
into:
$("#TestplanName").text( '#string.Format("{0}-{1}-{2}-{3}",releaseName,templateName,#Model.UserId,#Model.CreatedAt)');
The releaseName and templateName are unknown...
You can't.
You're trying to mix client-side variables – which only exist in Javascript on the client – with server-side code.
Instead, you can use a Javascript equivalent of string.Format.

Naming a text file on CSHTML (Razor)

I have a page with a text area for title input and body input.
Saving a text file with those things is easy, the question is, how can I make the file to be named after whatever was placed in the title input?
I tried this:
#{
var result = "";
if (IsPost)
{
var title = Request["title"];
var body = Request["body"];
var filedata = title + "," + body + Environment.NewLine;
var dataFile = Server.MapPath("/App_Data/Request["title"]");
File.WriteAllText(#dataFile, filedata);
result = "Information saved.";
}
}
(Note that var title = Request["title"]; means that its requesting from a text input named "title"). What I want to get is that the input will also be the name of the file its saving.
But it seems that this area:
var dataFile = Server.MapPath("/App_Data/Request["title"]");
is not the correct way.
What is the correct way to do it?
Couple of pointers; firstly this sort of logic should be in a Controller, not in a View. Your Views are supposed to display information about your model, your Controllers carry out operations.
Secondly, the following should do the trick (in a Controller!):
[HttpPost]
public ActionResult SaveFile(string title, string body)
{
var fileData = title + "," + body + Environment.NewLine;
var fileSavePath = Path.Combine(
Server.MapPath("~/TextFiles"),
title.Replace(" ", "_") + ".txt");
File.WriteAllText(fileSavePath, fileData);
return this.RedirectToAction("SaveSuccessful");
}
Of note:
Server.MapPath("~/TextFiles") gives you the path to a TextFiles directory in the root of your web application where the files will be stored.
I've replaced spaces in the title which has been input with underscores.
This method redirects the user to an Action named SaveSuccessful on the same Controller
Of course you need error handling and all sorts of other things in there, but hopefully that helps.

Resources