I have created the internet application ASP.NET MVC Using RAZOR.
I have written in Index.cshtml as:
#Session["my"]="Hello".
and I have written in AboutUs.cshtml as:
#Session["my"]
Still now I can't get the value from AboutUs.cshtml .
Instead of it in Index.cshtml it shows:
="Hello"
Replace #Session["my"]="Hello"; with #{ Session["my"] = "Hello"; }.
Note the addition of curly brackets.
Related
I'm converting my code to Asp.net core 3.1 in a web api project. my code is something similar to bellow:
When i call http://localhost:44307/PersonDetails I would like to get "Result1" and when i call http://localhost:44307/Person/2207 i should get "result2".
The first route works as i expected but for the second one, I get 404. it works if i call http://localhost:44307/PersonDetails/Person/2207 but it is not what i expected.
Appreciate if you could help me.
Try it like below:
[HttpGet]
[Route("~/Person/{personId}")]
public ActionResult<Person> Get([FromQuery]int personId)
{
return Ok("result2");
}
When you add ~ before it, it will ignore the route attribute on your controller.
[Route("[controller]")]
when you use a route on your controllers, all child actions will use that. so in this case, because your controller name is PersonDetails you have to enter the controller name in your URLs.
but, you can use this trick :
step1: remove this line:
[Route("[controller]")]
step2: add PersonDetails to your Get method
[Route("PersonDetails")]
Public ActionResult Get()
How do I retrieve the #odata.context value from a request to a aspnetcore 2.1 using odata v4 (microsoft aspnetcore.odata 7.0.1) inside the controller?
I needed to form my response as
{ "#odata.context":"https://localhost:5001/odata/$metadata#Products/$entity",
"value" :[{ "id":"b79a6aa2-ed65-400c-8f7d-42052412b0b4"}]
}
rather than ``{"#odata.context":"https://localhost:5001/odata/$metadata#Products/$entity","id":"b79a6aa2-ed65-400c-8f7d-42052412b0b4"}
Thank you!!
Alberto
It looks like your controller is returning a single value response, if you have it return an Enumerable then it'll be in the form you want
Microsoft has a good tutorial for getting your first ASP.NET Web API project going:
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
...but what's unclear to me -- how is the mapping between the API URIs and my C# controller methods defined?
for ex:
/api/products/{id}
resolves to
public IHttpActionResult GetProduct(int id)
{
[...]
}
...but im not sure how. automagic?
I ask because I want to create a new mapping for this URI:
/api/setReportNotificationsAsRead?uid={username}&items={itemIDs}
to this new method in my controller:
public IHttpActionResult SetReportNotificationsAsRead(string username, string itemIDs)
{
[...]
}
...but im not sure how. automagic?
The Automagic lies with HTTP Verbs in Web API. The route /api/products/{id} can map to anything that starts with GET (or just method name Get) if you make a GET request for the WebAPI.
If you make a POST request then it will map to any POST method (or else is you use route prefix).
This link can help you understand the magic. That can also help you understanding how you can configure SetReportNotificationsAsRead function to be called.
You're very close to having a working solution. You have your method parameter names mixed up:
public IHttpActionResult SetReportNotificationsAsRead(string uid, string items)
{
//Do your stuff!
}
The name of the HTTP GET parameter should match the name of the method parameter. This is part of the ASP.NET MVC naming convention.
One final note, if you are actually expecting a group of items (i.e. multiple item ids) you can actually save yourself some work and use a string[] parameter so long as your HTTP parameters are sent correctly. In this case you would want the URI to look like: /api/setReportNotificationsAsRead?items=123&items=456&items=789. Notice how the parameter name is repeated multiple times, but with different values.
For more information about the routing system I would suggest heading over to the ASP.NET MVC routing overview.
After migrating from mvc 3 to mvc 4 Razor v.2 wrong parse my js template.
Problem in the next {{if}} section
{{? (it.#Model.GetPropName(x => x.Children)).length < it.#Model.GetPropName(x => x.ChildTaskTotalCount)}}
{{?}}
Razor v2 parse second argument of condition(it.#Model.GetPropName(x => x.ChildTaskTotalCount) ) as a string. And dot.js throws error on client browser.
#Model.GetPropName()- function which returns name of model property.Other words it.#Model.GetPropName(x => x.ChildTaskTotalCount) equal to it.ChildTaskTotalCount, but (it.Children).length
This code block works correctly in asp.net mvc 3 with razor v.1.
How should i change syntax to correct work with razor v.2?
I was thinking about giving end users the ability to drop Partial Views (controls) into the information being stored in the database. Is there a way to execute a string I get from the database as part of the Razor view?
Update (I forgot all about this)
I had asked this question previously (which lead me to create RazorEngine) Pulling a View from a database rather than a file
I know of at least two: RazorEngine, MvcMailer
I have a bias towards RazorEngine as it's one that I've worked on but I have a much simpler one at Github called RazorSharp (though it only supports c#)
These are all pretty easy to use.
RazorEngine:
string result = RazorEngine.Razor.Parse(razorTemplate, new { Name = "World" });
MvcMailer
I haven't used this one so I can't help.
RazorSharp
RazorSharp also supports master pages.
string result = RazorSharp.Razor.Parse(new { Name = "World" },
razorTemplate,
masterTemplate); //master template not required
Neither RazorSharp, nor RazorEngine support any of the Mvc helpers such as Html and Url. Since these libraries are supposed to exist outside of Mvc and thus require more work to get them to work with those helpers. I can't say anything about MvcMailer but I suspect the situation is the same.
Hope these help.