Created the API using entity framework model to get the request data from other system and want to update those information into database.
//API
[AllowAnonymous]
public string UploadUser()
{
return "Welcome To SMART Web API";
}
and its accessible using: http://localhost:5958/api/UploadUser
Created the View Model to store the request data:
public class UserRequestViewModel
{
public UserRequestViewModel()
{
REQDTL = new List<Line>();
}
public List<Line> REQDTL { get; set; }
public int? REQLEN { get; set; }
public class Line
{
[Description("Id")]
public int? ID { get; set; }
public int? Id
{
get
{
return this.ID;
}
}
[Description("UserName")]
public int? USERNAME { get; set; }
public int? UserName
{
get
{
return this.USERNAME;
}
}
[Description("Password")]
public string PASSWORD { get; set; }
public string Password
{
get
{
return this.PASSWORD;
}
}
}
public class Summary
{
public int? Id { get; set; }
public int? UserName { get; set; }
public string Password { get; set; }
}
public List<Summary> GetSummary()
{
List<Summary> ret = this.REQDTL
.Where(x => x.Id != null ) //Filter to applicable records
.Select(x => new Summary()
{
Id = x.Id,
UserName = x.UserName,
Password = x.Password,
}
).ToList();
return ret;
}
}
}
My question is how to handle the below request data using my controller (Do I need to use PostAsync / GetAsync/ any other)
{"REQLEN":4,"REQDTL":[{"ID":"48490","USERNAME":"Test1","PASSWORD":"Test1"},{"ID":"48491","UserName":"Test2","Password":"Test2"}]}
In general, to accept a post and use the data to talk to a SQL database, if you are using Web API, then you first create a model for the data format that you expect:
public class MyModel
{
public int ID { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
Then you create an action in your controller like this:
public IHttpActionResult Post([FromBody]MyModel model)
{
if (ModelState.IsValid)
{
string myConnectionString = "...";
string mySqlCommand = "UPDATE MyTable SET...";
using (SqlConnection conn = new SqlConnection(myConnectionString))
using (SqlCommand cmd = new SqlCommand(mySqlCommand, conn))
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
return Ok();
}
else
return BadRequest("Invalid data");
}
(Of course, the sample data you posted looks more like a login attempt than something you would want to stick into a database, so I am guessing that you aren't really asking the right question. If you are just learning, I would recommend finding a tutorial that is similar to what you are trying to do. The ASP.Net templates have user authentication built in - it would be much more secure to use what they offer than to try to create your own if you are brand new to this type of work. If you just start from a blank ASP.Net project and click "Run", there will already be a way to login, sign up, etc if you do it right.)
Related
I try pass model data between two Razor pages, data is not string or bool or int
data that i want pass to second page is a model,
i do taht with this way,
public class AskLibrarian
{
public int Id { get; set; }
public string FullName { get; set; }
public string Subject { get; set; }
public string Email { get; set; }
public string Text { get; set; }
public string UserIp { get; set; }
public DateTime CreateDate { get; set; }
public bool ReadIt { get; set; }
public bool Answer { get; set; }
public string reciptCode { get; set; }
}
And on Get method pass data with this way:
[BindProperty]
public AskLibrarian AskLibrarian { get; set; }
public async Task<IActionResult> OnPostQuestionAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
AskLibrarian.Answer = false;
AskLibrarian.CreateDate = DateTime.Now;
AskLibrarian.ReadIt = false;
string userIp = $"{ HttpContext.Connection.RemoteIpAddress}";
if (string.IsNullOrEmpty(userIp))
{
userIp = "127.0.0.1";
}
AskLibrarian.UserIp = userIp;
string rndNuber = Business.RandomNumberForQuestion.randCode;
AskLibrarian.reciptCode = rndNuber;
await _emailSenderService.SendEmailAsync(AskLibrarian.Email, AskLibrarian.FullName, rndNuber);
_context.AskLibrarians.Add(AskLibrarian);
await _context.SaveChangesAsync();
Message = "your message sended";
//return RedirectToPage("/Subfolder/Index", new { SFId = 7 });
return RedirectToPage("/Subfolder/AskLibrarianCode", new { asklib = AskLibrarian });
}
In post method in second page, like to get data on this way:
public void OnGet(Model.AskLibrarian asklib)
{
askLibrarianVM = new AskLibrarianVM
{
Answered = false,
CreateDate = asklib.CreateDate,
LastUpdate = asklib.CreateDate,
RandomeCode = asklib.reciptCode,
Status = false,
};
}
But asklib is empty ,I set a breakpoint at end of Get method and I sow that asklib if filled with valid values but in post method when i try to get data, asklib is empty
what is my mistake
The simple answer was :
return RedirectToPage("/Subfolder/AskLibrarianCode", AskLibrarian );
My mistake was
... new{asklib = AskLibrarian});
After more than two hours
The lowest friction way is to return View("SomeViewForTheModel", AskLibrarian) and do your thing with a completely different view. Your second page controller action really isn't doing anything.
Otherwise, you'll have to save the ID associated with your AskLibrarian object, presumably in a database, and then look it up on the second page either by putting the ID in the URL path (be sure to validate the user should see it!), or by looking up in the database whatever is owned by the user.
My WebApi has a table for applications with the following class:
namespace Models.Public
{
[Index(nameof(UUID), nameof(UID), IsUnique = true)]
public class Application
{
public Application()
{
this.UUID = new Guid();
}
public int ID { get; set; }
public Guid UUID { get; set; }
[Required]
public string UID { get; set; }
public string Publisher { get; set; }
public string Name { get; set; }
public string Version { get; set; }
}
}
The field UUID and ID are unique, so I was able to generate the required HttpGet command to obtain the results matching for that.
However, I am trying to obtain an IEnumerable object of all the items that match the Publisher field. That is, return all object that have "Google" as their Publisher.
My attempts have not been successful and I am hoping for some advise to fix my code:
// GET: api/Application/<publisher>
[HttpGet("{publisher}")]
public async Task<ActionResult<IEnumerable<Application>>> GetApplication(string publisher)
{
var application = await _context.Application.ToListAsync(publisher);
if (application == null)
{
return NotFound();
}
return await _context.Application.ToListAsync();
}
Publisher is not a unique value, so I'd like to be able to return all items as a JSON object that have whatever Publisher I type in the list. If no matches, error handle with NotFound();.
You will need to filter using .Where, .Contains
// GET: api/Application/<publisher>
[HttpGet("{publisher}")]
public async Task<ActionResult<IEnumerable<ApplicationData>>> GetApplication(string publisher)
{
var applications = _context.Application.Where(a=>a.Publisher.Contains(publisher)));
/* You could also use == for exact match
var applications = _context.Application.Where(a=>a.Publisher == publisher));
*/
if (applications.Count() == 0)
{
return NotFound();
}
return await applications.ToListAsync();
}
I got an error while getting json data from POST method, am I doing something wrong
C# Code:
public IActionResult signupapi(UserSignUp user)
{
var model = new Models.SignUpModelAPI(HttpContext);
if (user == null)
{
return Content(model.ResponseJsonText(false, string.Format(model.Language("empty"),
HttpContext.Request.Method, HttpContext.Request.Path.Value), Class.CodeResponse.ERROR), new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
}
if (!model.isAllowMethod("POST"))
{
return Content(model.ResponseJsonText(false,string.Format(model.Language("notallowmethod"),
HttpContext.Request.Method,HttpContext.Request.Path.Value),Class.CodeResponse.ERROR),new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
}
return Content(JsonConvert.SerializeObject(user));
}
public class UserSignUp
{
public string fullname { get; set; }
public string username { get; set; }
public string email { get; set; }
public string password { get; set; }
}
And this is the result when i try on reqbin every value i get is null
You need to add FromBody attribute to get your data for the POST operation:
public IActionResult signupapi([FromBody]UserSignUp user)
You can read more on parameter binding on MSDN docs.
I have a web api built in .NET, within an endpoint i would like to redirect to an url which matches the the code inserted in database. the endpoint takes as entry the code and i am supposed to redirect to the corresponding url. For that i use the Redirect method which actually is not working. i did Console.Write to print if the url is null or empty but it exists. here is the code of my controller :
[HttpGet("{hash}")]
// [ProducesResponseType(302)]
//[ProducesResponseType(404)]
public async Task<IActionResult> redirectUrl(string hash)
{
var t = await new ServiceUrl(_ctx).GetTarget2(hash);
int a = 0;
foreach (Model.Data.DAO.Url i in t)
{
if (i != null)
{
a=a+1;
}
}
if (a==0)
{
return new TimeoutExceptionObjectResult(error: "Not Found",503);
}else
if (DateTime.Compare(DateTime.Now, t.ElementAt(0).ExpireAt) > 0)
{
t.ElementAt(0).state = "expire";
_ctx.Entry(t.ElementAt(0)).State = EntityState.Modified;
await _ctx.SaveChangesAsync();
return new TimeoutExceptionObjectResult(error: "Url expiré",501);
}
string url= t.ElementAt(0).UrlOrigin;
Console.Write(url);
return new Redirect(url);
}
the GetTarget2 method :
public async Task<IEnumerable<Url>> GetTarget2(string hash)
{
var t2 = await _ctx.Url.Where(u => u.UrlShort == hash).ToArrayAsync();
return t2;
}
and the entity :
[Table("Url")]
public class Url
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string UrlShort { get; set; }
public string UrlOrigin { get; set; }
public string state { get; set; }
public int Customer_id { get; set; }
public int? targetItemId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime ExpireAt { get; set; }
[JsonIgnore]
public List<Stats> GetStats { get; set; }
public Url()
{
}
public Url(int Id,string UrlShort,string UrlOrigin,string state,int Customer_id,DateTime CreatedAt,DateTime ExpireAt)
{
this.Id = Id;
this.UrlShort = UrlShort;
this.UrlOrigin = UrlOrigin;
this.state = state;
this.Customer_id = Customer_id;
this.CreatedAt = CreatedAt;
this.ExpireAt = ExpireAt;
}
}
when i try to pass a code which is in database i get this : Not found which means it does not find it in database
Update:
the database context declaration :
private readonly DatabaseContext _ctx;
and its definition :
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public DbSet<Url> Url { get; set; }
public DbSet<User> User { get; set; }
public DbSet<Stats> Stats { get; set; }
}
If I understood your question correctly then you are trying to redirect to another action method by reading the URL from the database. If the retured Url belongs to your application and just have the dynamic parameter then you can try RedirectToAction method for the redirect.
Syntax:
return RedirectToAction("ActionName", new { id = 90 });
The will cause a redirect to YourApp/Controller/ActionName/90
All you have to create an action method with argument (if required) and redirect from another action method from where you are getting url.
Alternativly you can also use
return RedirectToAction("Index", new RouteValueDictionary(
new { controller = "NameOfController", action = "Index", id = id } )
);
The problem was the Redirect method which indicates its content was null or empty although i printed the url in terminal successfully. So What i did is adding a prefix (http://) like this Redirect("http://"+url), url is the url coming from the database. if the url already contains the prefix http or https i remove it before passing it as parameter inside Redirect method. Doing this solved my problem.
I am new to MVC and working on Login Page validation, and having problem in controller for fetching login user data through a stored procedure with an .edmx file.
PROC_LogIn_Info is the name of the stored procedure.
I get an error :
Cannot convert method group 'ToList' to non-delegate type 'System.Collections.Generic.List'. Did you intend to invoke the method?
Code:
[HttpPost]
public ActionResult Login(tblUserMaintenance user)
{
if (ModelState.IsValid)
{
using (dbPA_MVCEntities objCon = new dbPA_MVCEntities())
{
List<Login> LoginUser = objCon.PROC_LogIn_Info("jhony", "a").ToList();
// showing Error here
}
}
else
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return RedirectToAction("LoginIndex", "Login");
}
My model is Login:
public class Login
{
[Required]
[DisplayName("User ID")]
public string vUserID { get; set; }
[DisplayName("User Name")]
public string vUserName { get; set; }
[DisplayName("User Email Id")]
public string vUserEmail { get; set; }
[DisplayName("Phone Number")]
public string vPhoneNumber { get; set; }
public string vRoleId { get; set; }
public string IsActive { get; set; }
public string chUserType { get; set; }
[Required]
[DisplayName("Password")]
public string vPassword { get; set; }
public List<Login> LoginUsersData { get; set; }
}
First of all make a reference to System.Linq in your cs file, then use ToList from System.Linq :
using System;
using System.Collections.Generic;
using System.Linq;
public IList<ActivityEvent> GetActivityEvent(string sESSION_ID)
{
var result = this.unitOfWork.Context.GetActivityEvent(sESSION_ID);
if (result != null)
{
return result.ToList();
}
return null;
}
I Searched many Articles but didn't see a proper answer for this. So, i have created a sample piece of code. Hope this helps.
static void Main(string[] args)
{
List<User> myUserList = GetUserList();
}
public static List<User> GetUserList()
{
var dbContext = new UserEntities();
var results = dbContext.GetUserDetails();
return results.Select(x => new User
{
User_id = x.User_id,
First_Name = x.First_Name,
Last_Name = x.Last_NAME
}).ToList();
}
In the above piece of code, dbContext is the edmx context reference. UserEntities() is the name of my edmx instance. GetUserDetails() is the name of stored procedure and User is the model class. HOPE THIS HELPS :)
References:
https://forums.asp.net/t/2072373.aspx?Can+not+implicitly+convert+List+objectresult+T+to+collection+GenericList+T+
The result from a stored procedure is a unique, autogenerated type. The simplest way to do the conversion is
List<Login> LoginUser = from result in objCon.PROC_LogIn_Info("jhony", "a").ToList()
select new Login
{
// Set each property in turn
vUserID = result.UserId;
...
};
by using complex type of PROC_LogIn_Info_Result
[HttpPost]
public ActionResult Login(Login user)
{
if (ModelState.IsValid)
{
using (dbPA_MVCEntities objCon = new dbPA_MVCEntities())
{
List<PROC_LogIn_Info_Result> LoginUser = objCon.PROC_LogIn_Info(user.vUserid, user.vPassword).ToList<PROC_LogIn_Info_Result>();
if (LoginUser.Count > 0)
{
Session["UserID"] = LoginUser[0].vUserid;
Session["UserName"] = LoginUser[0].vUserName;
Session["UserRole"] = LoginUser[0].vRole;
Session["UserType"] = LoginUser[0].vUserType;
Session["UserEmailId"] = LoginUser[0].vUserEmail;
}
else
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
else
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return RedirectToAction("LoginIndex", "Login");
}