Handling missing and null parameters in asp.net mvc [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new to asp.net mvc.
now the requirement is that I have to handle following URLs in same ActionResult
if HTTP//jpripu/Handset/shop_code=&hosho_date= then do something.
if HTTP//jpripu/Handset/shop_code=Cust01&hosho_date=20131212 then do something
if HTTP//jpripu/Handset/hosho_date= then do something
if HTTP//jpripu/Handset/shop_code= then do something
Is it feasible to execute above conditions separately?
could anybody help me on this. Thanks.

If you mean URLs like http://jpripu/shop_code=&hosho_date=20130923
then this controller is for you:
public class HandsetController : Controller
{
public ActionResult Index(string shop_code, string hosho_date)
{
ViewBag.shop_code = shop_code;
ViewBag.hosho_date = hosho_date;
return View();
}
}
Note: Index - is default Action, defined in your routing.
Also I suggest Pluralsight Introduction to ASP.NET MVC 3 screencasts as a quick-start quide to ASP.NET MVC.

Related

How to pass post values to webapi .net core [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to post datetime into the below api. But it is not going through.
lastRefreshDateTime.Result is datetime
Below is the code I tried:
await client.PostAsync($"{_printApiUrl}pdf/GenerateAndEmailZipOfPDFs/${lastRefreshDateTime.Result}", content, cancellationToken);
[Authorize]
[Route("api/pdf/GenerateAndEmailZipOfPDFs/{lastRefreshDateTime}")]
public void GenerateAndEmailZipOfPDFs([FromBody]List<UrlObject> urls,DateTime lastRefreshDateTime)
You are most likely experiencing a deadlock by mixing a blocking call like .Result in an async function.
You need to use await.
var lastRefreshDateTime = await _contentManagement.GetLastRefreshDateTime(cancellationToken);
Reference Async/Await - Best Practices in Asynchronous Programming
Also note how the URI is generated using $ - string interpolation
var uri = $"{_printApiUrl}pdf/GenerateAndEmailZipOfPDFs/{lastRefreshDateTime}";
await client.PostAsync(uri, content, cancellationToken);
There was an errant $ in the shown URI that would cause the date time to be malformed when posted.
Should also consider using a route constraint
[Authorize]
[Route("api/pdf/GenerateAndEmailZipOfPDFs/{lastRefreshDateTime:datetime}")]
public void GenerateAndEmailZipOfPDFs(
[FromBody]List<UrlObject> urls,
[FromRoute]DateTime lastRefreshDateTime
)
Reference Routing to controller actions in ASP.NET Core
Reference Routing in ASP.NET Core

How to invoke the JavaScript function from code behind (c# / controller)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
In MVC, Controller - I will have to enable or disable the text box in the page load event.
Mentioned line of syntax is written inside a JS file / function.
document.getElementById('ShowTextBox').disabled = false;
How to invoke the JavaScript function from code behind (c# / controller)?
Arg, too many comments:
if you have a simple need to send some result of server side processing to your View, one time (onload), then you have ViewBag and/or ViewData
Ajax/XHR for more processing
even partials if you ever have to (partial views with javascript mixed in with c# or vb stuff)
<textarea id="ShowTextBox" asp-for="ShowTextBox" style="height:150px;" class="form-control"></textarea>
Trivial sample only improve as needed:
Controller:
public ActionResult Index()
{
//some server side processing....
ViewBag.Foo = DateTime.Now.Second % 2 == 0;
return View();
}
View (index.cshtml):
<textarea id="ShowTextBox" asp-for="ShowTextBox" style="height:150px;" class="form-control"></textarea>
<script>
// trivial example only
document.getElementById('ShowTextBox').disabled = #ViewBag.Foo;
</script>
REF:
Passing data to views - doc is for Core, but same concept.

Use of IdentityBasicAuthenticationAttribute in MVC 4 API [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How can I add the IdentityBasicAuthenticationAttribute in my project? I have read the link below shared by the #IonutUngureanu, but some of the steps are skipped in the document.
Please check the attached screenshot for the error.
Thank You.
You could use or create a custom Authentication/Authorize filter attribute:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/security/authentication-filters
From your picture I see that you are missing the implementation of the IdentityBasicAuthenticationAttribute class. Step by step instructions:
Create a folder called Filters
In the folder Filters create a class called IdentityBasicAuthenticationAttribute :
public class IdentityBasicAuthenticationAttribute : BasicAuthenticationAttribute
{
protected override async Task<IPrincipal> AuthenticateAsync(string userName, string password, CancellationToken cancellationToken)
{
// Implement logic suitable for your case
return new ClaimsPrincipal(identity);
}
}
Register your filter:
config.Filters.Add(new IdentityBasicAuthenticationAttribute());
Full code sample here: http://aspnet.codeplex.com/sourcecontrol/latest#Samples/WebApi/BasicAuthentication/BasicAuthentication/Filters/IdentityBasicAuthenticationAttribute.cs

How to make website readonly that link from certain websites. asp.net [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a website where a user logs in, can see his information and edit it. There is also another website, something like a site only for admins, where if I click a link it redirects me to the first website, logged in as that user but I can only read his information not edit it. I am having trouble finding out how to make website 1 both readable only and read/writeable.
I am doing this in Asp.NET mvc using C#.
One method would be to check for authorization in the Razor view.
Psuedocode:
#if(User.IsAuthorizedForEdit()
{
#*your edit view code*#
}
else
{
#*your readonly view code*#
}
This does make for some bloaty Razor. The other (arguably, better) alternative is to direct them to the appropriate view in your controller based on user.
Handy-wavy psuedocode to give you an idea:
public ActionResult ViewProfile(int profileId)
{
var user = GetCurrentUser();//without looking at your code, I can't infer this piece.
var profile = GetProfile(profileId);
if(IsAuthorizedToEdit(user, profileId)
{
return View("edit", profile);
}
else
{
return View("view", profile);
}
}
In theory, you already have a read-only view and an edit view, so the latter would be more reusable.

My custom attribute not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make an attribute so that I can decorate an action/controller and have some code run.
This is the code for my Attribute:
public class UpdateLastLogInAttribute : System.Attribute
{
Linq.UserMinData umd { get; set; }
public UpdateLastLogInAttribute()
{
this.umd =datafuncs.GetMinData();
if (umd != null)
datafuncs.SaveLastConnected(umd);
}
}
...and this is the Controller that I want the Attribute to work with
[funcs.UpdateLastLogIn]
public class HomeController : Controller
{
}
However, when I hit the controller, my code never executes. What's wrong?
Attributes are dumb. There isn't any magic that makes code in attributes run. It's down to you to reflect on your code and to detect the attributes and run the code.
Fortunately, MVC already does this for specific attribute types, so...
You might consider extending ActionFilterAttribute and overriding OnActionExecuting,OnActionExecuted, OnResultExecuting or OnResultExecuted depending on which phase of the request you want to intercept.
MVC looks out for subclasses of this attribute and executes the four methods above at the appropriate time.

Categories